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>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。