javascript - script is preventing anchor link from appearing in URL -
i'm using jquery came across smooth scrolling anchor links within page. realize there in script preventing anchor link (e.g., '#top') appearing in url -- , want anchor link in url. can tell me part of turning off default behavior , turn on?
<script> $(function() { $('a[href*="#"]:not([href="#"])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { $('html, body').animate({ scrolltop: target.offset().top }, 500); return false; } } }); }); </script>
i have not tested out on real page, on end of animate, can set hash.
$(function() { $('a[href*="#"]:not([href="#"])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var hash = this.hash, target = $(hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { $('html, body').animate({ scrolltop: target.offset().top }, 500, function(){ window.location.hash = hash; }); return false; } } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <a href="#foo">bottom</a><br/> a<br/>b<br/>c<br/>d<br/> e<br/>f<br/>g<br/>h<br/> i<br/>j<br/>k<br/>l<br/> m<br/>n<br/>o<br/>p<br/> q<br/>r<br/>s<br/>t<br/> u<br/>v<br/>w<br/>x<br/> y<br/>z<br/> <p id="foo">hey</p>
Comments
Post a Comment