随着一页网站的出现,使用滚动做为导航长页面的方法变得愈来愈流行。这一小部分是用JS + jQuery代码实现的,您能够轻松地在nav元素中设置连接以滚动到页面的相应部分。若是您但愿在JS不存在时将锚标记添加到页面中。html
Coffeescript:html5
$("nav").find("a").click (e) -> e.preventDefault() section = $(this).attr "href" $("html, body").animate scrollTop: $(section).offset().top
或JS:java
$("nav").find("a").click(function(e) { e.preventDefault(); var section = $(this).attr("href"); $("html, body").animate({ scrollTop: $(section).offset().top });});
和一些示例HTMLlinux
<nav> <a href="#welcome">Welcome</a> <a href="#about">About</a> <a href="#section3">Section 3</a> </nav> <div id="welcome">Welcome to this website</div> <div id="about">About this website, and such</div> <div id="section3">The third section</div>
以上就是如何使用<nav>连接实现滚动到页面某一部分的详细内容,但愿对你有所帮助。web
阅读原文:如何使用nav连接实现滚动到页面某一部分segmentfault
相关文章:
使用jQuery HTML5和FormData上传文件的方法示例
浅谈H5的data-*中容易被忽略的一个小问题
html5须要遵照的6个原则(介绍)
linux怎么挂载
java能够对map的key排序吗网站