Written with bootstrap. The following is the dry information: The href in each a of each li of ul points to the anchor target, for example, <li class="active"><a href= "#index">Home</a></li>.
The following is the source code I found:
$('#navbar-menu ul li a[href^="#"]').click(function(e){ e.preventDefault(); $('html, body').animate({scrollTop: $(this.hash).offset().top}, 400); });
Translation: When clicked, the default behavior is first blocked, and then the html, and body scrolling animations are scrolled to The distance from the top is the distance of the target's offset().top, which is completed in 4 milliseconds. The hash attribute is a readable and writable string, which is the anchor part of the URL (the part starting from the # sign), so this.hash refers to the # part of the current address. Regarding the object represented by this, we will summarize it later.
ps:$('body').scrollspy({ target: '#menu-nav' })
This bootstrap's scrollspy plug-in automatically monitors the target scrolling and changes the corresponding active, which is very good of.
Related recommendations:
Javascript implementation of navigation anchor scrolling effect
The above is the detailed content of js anchor scrolling example sharing. For more information, please follow other related articles on the PHP Chinese website!