jquery scrollTop方法根據(jù)滾動像素顯示隱藏頂部導航條
更新時間:2013年05月27日 17:31:13 作者:
使用jquery的scrollTop方法監(jiān)視頁面垂直滾動像素,并根據(jù)像素隱藏或者顯示頂部的導航條,具體實現(xiàn)代碼如下,感興趣的朋友可以參考下哈
本文使用jquery的scrollTop方法監(jiān)視頁面垂直滾動像素,并根據(jù)像素隱藏或者顯示頂部的導航條。
效果預覽網(wǎng)址:http://www.keleyi.com/keleyi/phtml/scrolloversee.htm
scrollTop的介紹:
語法
$(selector).scrollTop(offset)
參數(shù)
offset
描述
可選。規(guī)定相對滾動條頂部的偏移,以像素計。
定義和用法
scrollTop() 方法返回或設(shè)置匹配元素的滾動條的垂直位置。
scroll top offset 指的是滾動條相對于其頂部的偏移。
如果該方法未設(shè)置參數(shù),則返回以像素計的相對滾動條頂部的偏移。
提示和注釋
注釋:該方法對于可見元素和不可見元素均有效。
注釋:當用于獲取值時,該方法只返回第一個匹配元素的 scroll top offset。
注釋:當用于設(shè)置值時,該方法設(shè)置所有匹配元素的 scroll top offset。
附完整代碼,保存到html文件,打開可以體驗效果:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.keleyi.com/1999/xhtml">
<head>
<title>監(jiān)視滾動像素,顯示或隱藏頂部導航條-柯樂義</title>
<style type="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>
<script type="text/javascript" src="http://www.keleyi.com/keleyi/pmedia/jquery-1.9.1.min.js"></script>
</head>
<body>
<div id="topmenu_keleyi_com"><ul><li><a target="_blank">.NET</a></li><li><a target="_blank">Javascript</a></li><li><a target="_blank">jQuery</a></li><li><a target="_blank">C#</a></li><li><a target="_blank">其他</a></li><li><a target="_blank">首頁</a></li></ul></div>
<div style="background-color:#959822; width:100%;height:150px;">請滾動鼠標使頁面向下</div>
<div style="background-color:Green; width:100%;height:150px;">www.keleyi.com</div>
<div style="background-color:Red; width:100%;height:150px;">歡迎</div>
<div style="background-color:Yellow; width:100%;height:150px;">hi</div>
<div style="background-color:Silver; width:100%;height:150px;">柯樂義</div>
<div style="background-color:Aqua; width:100%;height:150px;">keleyi.com</div>
<div style="background-color:Fuchsia; width:100%;height:150px;">keleyi</div>
<div style="background-color:Green; width:100%;height:150px;">keleyi.com</div>
<div style="background-color:Blue; width:100%;height:150px;">柯樂義</div>
<div style="background-color:Olive; width:100%;height:150px;">柯樂義 監(jiān)視下滾像素</div>
<div style="background-color:Green; width:100%;height:150px;">A</div>
<div style="background-color:Purple; width:100%;height:150px;">jquery</div>
<div style="background-color:Green; width:100%;height:150px;"><a target="_blank">原文</a></div>
<div style="background-color:Lime; width:100%;height:150px;">keleyi.com</div>
<div style="background-color:Orange; width:100%;height:150px;">完整代碼</div>
<script type="text/javascript">
$(function () {
//當滾動條的位置處于距頂部200像素以下時,頂部導航出現(xiàn),否則消失
$(function () {
$(window).scroll(function () {
if ($(window).scrollTop() > 200) {
$("#topmenu_keleyi_com").fadeIn(100);
}
else {
$("#topmenu_keleyi_com").fadeOut(100);
}
});
});
});
</script>
</body>
</html>
效果預覽網(wǎng)址:http://www.keleyi.com/keleyi/phtml/scrolloversee.htm
scrollTop的介紹:
語法
$(selector).scrollTop(offset)
參數(shù)
offset
描述
可選。規(guī)定相對滾動條頂部的偏移,以像素計。
定義和用法
scrollTop() 方法返回或設(shè)置匹配元素的滾動條的垂直位置。
scroll top offset 指的是滾動條相對于其頂部的偏移。
如果該方法未設(shè)置參數(shù),則返回以像素計的相對滾動條頂部的偏移。
提示和注釋
注釋:該方法對于可見元素和不可見元素均有效。
注釋:當用于獲取值時,該方法只返回第一個匹配元素的 scroll top offset。
注釋:當用于設(shè)置值時,該方法設(shè)置所有匹配元素的 scroll top offset。
附完整代碼,保存到html文件,打開可以體驗效果:
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.keleyi.com/1999/xhtml">
<head>
<title>監(jiān)視滾動像素,顯示或隱藏頂部導航條-柯樂義</title>
<style type="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>
<script type="text/javascript" src="http://www.keleyi.com/keleyi/pmedia/jquery-1.9.1.min.js"></script>
</head>
<body>
<div id="topmenu_keleyi_com"><ul><li><a target="_blank">.NET</a></li><li><a target="_blank">Javascript</a></li><li><a target="_blank">jQuery</a></li><li><a target="_blank">C#</a></li><li><a target="_blank">其他</a></li><li><a target="_blank">首頁</a></li></ul></div>
<div style="background-color:#959822; width:100%;height:150px;">請滾動鼠標使頁面向下</div>
<div style="background-color:Green; width:100%;height:150px;">www.keleyi.com</div>
<div style="background-color:Red; width:100%;height:150px;">歡迎</div>
<div style="background-color:Yellow; width:100%;height:150px;">hi</div>
<div style="background-color:Silver; width:100%;height:150px;">柯樂義</div>
<div style="background-color:Aqua; width:100%;height:150px;">keleyi.com</div>
<div style="background-color:Fuchsia; width:100%;height:150px;">keleyi</div>
<div style="background-color:Green; width:100%;height:150px;">keleyi.com</div>
<div style="background-color:Blue; width:100%;height:150px;">柯樂義</div>
<div style="background-color:Olive; width:100%;height:150px;">柯樂義 監(jiān)視下滾像素</div>
<div style="background-color:Green; width:100%;height:150px;">A</div>
<div style="background-color:Purple; width:100%;height:150px;">jquery</div>
<div style="background-color:Green; width:100%;height:150px;"><a target="_blank">原文</a></div>
<div style="background-color:Lime; width:100%;height:150px;">keleyi.com</div>
<div style="background-color:Orange; width:100%;height:150px;">完整代碼</div>
<script type="text/javascript">
$(function () {
//當滾動條的位置處于距頂部200像素以下時,頂部導航出現(xiàn),否則消失
$(function () {
$(window).scroll(function () {
if ($(window).scrollTop() > 200) {
$("#topmenu_keleyi_com").fadeIn(100);
}
else {
$("#topmenu_keleyi_com").fadeOut(100);
}
});
});
});
</script>
</body>
</html>
您可能感興趣的文章:
- jQuery實現(xiàn)的導航條切換可顯示隱藏
- 用jQuery實現(xiàn)一些導航條切換,顯示隱藏的實例代碼
- JQuery實現(xiàn)鼠標滑過顯示導航下拉列表
- 基于jquery實現(xiàn)導航菜單高亮顯示(兩種方法)
- jquery實現(xiàn)的讓超出顯示范圍外的導航自動固定屏幕最頂上
- 基于jquery實現(xiàn)頁面滾動時頂部導航顯示隱藏
- jQuery實現(xiàn)左側(cè)導航模塊的顯示與隱藏效果
- jquery實現(xiàn)隱藏與顯示動畫效果/輸入框字符動態(tài)遞減/導航按鈕切換
- php+jQuery實現(xiàn)的三級導航欄下拉菜單顯示效果
- jQuery實現(xiàn)的導航條點擊后高亮顯示功能示例
相關(guān)文章
jQuery移除tr無效的解決方法(tr是動態(tài)添加)
移除掉某些tr(tr是動態(tài)添加的)嘗試了很多方法,都不見效,后來發(fā)現(xiàn)個不錯的方法,于是與大家分享下2014-09-09
jQuery常用知識點總結(jié)以及平時封裝常用函數(shù)
這篇文章主要介紹了jQuery常用知識點總結(jié)以及平時封裝常用函數(shù) 的相關(guān)資料,需要的朋友可以參考下2016-02-02
jQuery數(shù)據(jù)類型小結(jié)(14個)
jQuery除了包含原生JS中的內(nèi)置數(shù)據(jù)類型(built-in datatype),還包括一些擴展的數(shù)據(jù)類型(virtual types),如Selectors、Events等,通過本文給大家分享14個jquery數(shù)據(jù)類型,感興趣的朋友一起學習吧2016-01-01

