随着网站页面的出现,使用滚动作为导航长页面的方法变得越来越流行。使用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>Welcome</a> <a>About</a> <a>Section 3</a> </nav><div>Welcome to this website</div> <div>About this website, and such</div> <div>The third section</div>
本篇文章到这里就已经全部结束了,更多精彩内容大家可以关注PHP中文网的HTML视频教程栏目!!!











