随着一页网站的出现,使用滚动作为导航长页面的方法变得越来越流行。这一小部分是用js + jquery代码实现的,您可以轻松地在nav元素中设置链接以滚动到页面的相应部分。如果您希望在js不存在时将锚标记添加到页面中。
Coffeescript:
$("nav").find("a").click (e) -> e.preventDefault() section = $(this).attr "href" $("html, body").animate scrollTop: $(section).offset().top
或JS:
$("nav").find("a").click(function(e) { e.preventDefault(); var section = $(this).attr("href"); $("html, body").animate({ scrollTop: $(section).offset().top });});
和一些示例HTML
<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>链接实现滚动到页面某一部分的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号