本文摘自PHP中文网,作者不言,侵删。
随着网站页面的出现,使用滚动作为导航长页面的方法变得越来越流行。使用JS + jQuery代码,可以轻松地在nav元素中设置链接以滚动到页面的相应部分。如果你希望在JS不存在时很好地降级,请将锚标记添加到页面中,本篇文章就来给大家介绍关于使用<nav>链接滚动到页面相应部分。

下面是具体的代码
Coffeescript:
1 2 3 4 5 | $( "nav" ).find( "a" ).click (e) ->
e.preventDefault()
section = $( this ).attr "href"
$( "html, body" ).animate
scrollTop: $(section).offset().top
|
或JS代码:
1 2 3 4 5 6 7 | $( "nav" ).find( "a" ).click( function (e) {
e.preventDefault();
var section = $( this ).attr( "href" );
$( "html, body" ).animate({
scrollTop: $(section).offset().top
});
});
|
HTML代码:
1 2 3 4 5 6 7 8 | < 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 >
|
本篇文章到这里就已经全部结束了,更多精彩内容大家可以关注PHP中文网的HTML视频教程栏目!!!
以上就是使用<nav>链接滚动到页面相应部分的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
css如何实现禁止选择文本
jquery实现带弹窗和次数的转盘抽奖(代码)
css border-bottom-style属性怎么用
css如何清除定位
css怎么隐藏table
css怎么设置间距
css的min-height属性怎么用
css如何设置字体平滑
css3如何实现聊天气泡效果?(代码示例)
css怎么让文字不换行
更多相关阅读请进入《css》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 使用<nav>链接滚动到页面相应部分