欢迎您来到懒之才-站长的分享平台!   学会偷懒,并懒出境界是提高工作效率最有效的方法!
首页 > 经验分享 > Jquery > jquery scrollTop方法根据滚动像素显示隐藏顶部导航条

jquery scrollTop方法根据滚动像素显示隐藏顶部导航条

2018-05-14 559 收藏 0 赞一个 0 真差劲 0 去评论

scrollTop的介绍:

语法

$(selector).scrollTop(offset)

参数

offset

描述

可选。规定相对滚动条顶部的偏移,以像素计。

定义和用法

scrollTop() 方法返回或设置匹配元素的滚动条的垂直位置。

scroll top offset 指的是滚动条相对于其顶部的偏移。

如果该方法未设置参数,则返回以像素计的相对滚动条顶部的偏移。

提示和注释

注释:该方法对于可见元素和不可见元素均有效。

注释:当用于获取值时,该方法只返回第一个匹配元素的 scroll top offset。

注释:当用于设置值时,该方法设置所有匹配元素的 scroll top offset。

附完整代码,保存到html文件,打开可以体验效果:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.keleyi.com/1999/xhtml">
<head>
<title>监视滚动像素,显示或隐藏顶部导航条-柯乐义</title>
<styletype="text/css">
#topmenu_keleyi_com{position:fixed;top:0px;left:0px;right:0px;width:800px;margin-left:auto;margin-right:auto;height:50px;background-color:transparent;display:none;}   
#topmenu_keleyi_com li{float:left;   
list-style:none;   
margin-right:1px;   
width:116px;   
background-color:#e0f0e0;   
}   
#topmenu_keleyi_com ul{ margin:0px;padding:0px;}   
body{margin:0px;}   
</style>
<scripttype="text/javascript"src="http://www.keleyi.com/keleyi/pmedia/jquery-1.9.1.min.js"></script>
</head>
<body>
<divid="topmenu_keleyi_com"><ul><li><ahref="http://www.keleyi.com/menu/net/"target="_blank">.NET</a></li><li><ahref="http://www.keleyi.com/menu/javascript/"target="_blank">Javascript</a></li><li><ahref="http://www.keleyi.com/menu/jquery/"target="_blank">jQuery</a></li><li><ahref="http://www.keleyi.com/menu/csharp/"target="_blank">C#</a></li><li><ahref="http://www.keleyi.com/menu/other/"target="_blank">其他</a></li><li><ahref="http://www.keleyi.com/"target="_blank">首页</a></li></ul></div>
<divstyle="background-color:#959822; width:100%;height:150px;">请滚动鼠标使页面向下</div>
<divstyle="background-color:Green; width:100%;height:150px;">www.keleyi.com</div>
<divstyle="background-color:Red; width:100%;height:150px;">欢迎</div>
<divstyle="background-color:Yellow; width:100%;height:150px;">hi</div>
<divstyle="background-color:Silver; width:100%;height:150px;">柯乐义</div>
<divstyle="background-color:Aqua; width:100%;height:150px;">keleyi.com</div>
<divstyle="background-color:Fuchsia; width:100%;height:150px;">keleyi</div>
<divstyle="background-color:Green; width:100%;height:150px;">keleyi.com</div>
<divstyle="background-color:Blue; width:100%;height:150px;">柯乐义</div>
<divstyle="background-color:Olive; width:100%;height:150px;">柯乐义 监视下滚像素</div>
<divstyle="background-color:Green; width:100%;height:150px;">A</div>
<divstyle="background-color:Purple; width:100%;height:150px;">jquery</div>
<divstyle="background-color:Green; width:100%;height:150px;"><ahref="http://www.keleyi.com/a/bjac/f4a6f78d74a251c5.htm"target="_blank">原文</a></div>
<divstyle="background-color:Lime; width:100%;height:150px;">keleyi.com</div>
<divstyle="background-color:Orange; width:100%;height:150px;">完整代码</div>
<scripttype="text/javascript">
$(function () {   
//当滚动条的位置处于距顶部200像素以下时,顶部导航出现,否则消失   
$(function () {   
$(window).scroll(function () {   
if ($(window).scrollTop() > 200) {   
$("#topmenu_keleyi_com").fadeIn(100);   
}   
else {   
$("#topmenu_keleyi_com").fadeOut(100);   
}   
});   
});   
});   
</script>
</body>
</html>


一、推荐使用迅雷或快车等多线程下载软件下载本站资源。

二、未登录会员无法下载,登录后可获得更多便利功能,若未注册,请先注册。

三、如果服务器暂不能下载请稍后重试!总是不能下载,请点我报错 ,谢谢合作!

四、本站大部分资源是网上搜集或私下交流学习之用,任何涉及商业盈利目的均不得使用,否则产生的一切后果将由您自己承担!本站将不对任何资源负法律责任.如果您发现本站有部分资源侵害了您的权益,请速与我们联系,我们将尽快处理.

五、如有其他问题,请加网站设计交流群(点击这里查看交流群 )进行交流。

六、如需转载本站资源,请注明转载来自并附带链接

七、本站部分资源为加密压缩文件,统一解压密码为:www.aizhanzhe.com

大家评论