Home > Web Front-end > JS Tutorial > js anchor scrolling example sharing

js anchor scrolling example sharing

小云云
Release: 2018-03-12 16:09:54
Original
1339 people have browsed it

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:

$(&#39;#navbar-menu ul li a[href^="#"]&#39;).click(function(e){
                    e.preventDefault();
                    $(&#39;html, body&#39;).animate({scrollTop: $(this.hash).offset().top}, 400);
                });
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template