jquery scrollTop方法根據(jù)滾動(dòng)像素顯示隱藏頂部導(dǎo)航條
更新時(shí)間:2013年05月27日 17:31:13 作者:
使用jquery的scrollTop方法監(jiān)視頁(yè)面垂直滾動(dòng)像素,并根據(jù)像素隱藏或者顯示頂部的導(dǎo)航條,具體實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈
本文使用jquery的scrollTop方法監(jiān)視頁(yè)面垂直滾動(dòng)像素,并根據(jù)像素隱藏或者顯示頂部的導(dǎo)航條。
效果預(yù)覽網(wǎng)址:http://www.keleyi.com/keleyi/phtml/scrolloversee.htm
scrollTop的介紹:
語(yǔ)法
$(selector).scrollTop(offset)
參數(shù)
offset
描述
可選。規(guī)定相對(duì)滾動(dòng)條頂部的偏移,以像素計(jì)。
定義和用法
scrollTop() 方法返回或設(shè)置匹配元素的滾動(dòng)條的垂直位置。
scroll top offset 指的是滾動(dòng)條相對(duì)于其頂部的偏移。
如果該方法未設(shè)置參數(shù),則返回以像素計(jì)的相對(duì)滾動(dòng)條頂部的偏移。
提示和注釋
注釋:該方法對(duì)于可見(jiàn)元素和不可見(jiàn)元素均有效。
注釋:當(dāng)用于獲取值時(shí),該方法只返回第一個(gè)匹配元素的 scroll top offset。
注釋:當(dāng)用于設(shè)置值時(shí),該方法設(shè)置所有匹配元素的 scroll top offset。
附完整代碼,保存到html文件,打開(kāi)可以體驗(yàn)效果:
<!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)視滾動(dòng)像素,顯示或隱藏頂部導(dǎo)航條-柯樂(lè)義</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">首頁(yè)</a></li></ul></div>
<div style="background-color:#959822; width:100%;height:150px;">請(qǐng)滾動(dòng)鼠標(biāo)使頁(yè)面向下</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;">柯樂(lè)義</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;">柯樂(lè)義</div>
<div style="background-color:Olive; width:100%;height:150px;">柯樂(lè)義 監(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 () {
//當(dāng)滾動(dòng)條的位置處于距頂部200像素以下時(shí),頂部導(dǎo)航出現(xiàn),否則消失
$(function () {
$(window).scroll(function () {
if ($(window).scrollTop() > 200) {
$("#topmenu_keleyi_com").fadeIn(100);
}
else {
$("#topmenu_keleyi_com").fadeOut(100);
}
});
});
});
</script>
</body>
</html>
效果預(yù)覽網(wǎng)址:http://www.keleyi.com/keleyi/phtml/scrolloversee.htm
scrollTop的介紹:
語(yǔ)法
$(selector).scrollTop(offset)
參數(shù)
offset
描述
可選。規(guī)定相對(duì)滾動(dòng)條頂部的偏移,以像素計(jì)。
定義和用法
scrollTop() 方法返回或設(shè)置匹配元素的滾動(dòng)條的垂直位置。
scroll top offset 指的是滾動(dòng)條相對(duì)于其頂部的偏移。
如果該方法未設(shè)置參數(shù),則返回以像素計(jì)的相對(duì)滾動(dòng)條頂部的偏移。
提示和注釋
注釋:該方法對(duì)于可見(jiàn)元素和不可見(jiàn)元素均有效。
注釋:當(dāng)用于獲取值時(shí),該方法只返回第一個(gè)匹配元素的 scroll top offset。
注釋:當(dāng)用于設(shè)置值時(shí),該方法設(shè)置所有匹配元素的 scroll top offset。
附完整代碼,保存到html文件,打開(kāi)可以體驗(yàn)效果:
復(fù)制代碼 代碼如下:
<!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)視滾動(dòng)像素,顯示或隱藏頂部導(dǎo)航條-柯樂(lè)義</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">首頁(yè)</a></li></ul></div>
<div style="background-color:#959822; width:100%;height:150px;">請(qǐng)滾動(dòng)鼠標(biāo)使頁(yè)面向下</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;">柯樂(lè)義</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;">柯樂(lè)義</div>
<div style="background-color:Olive; width:100%;height:150px;">柯樂(lè)義 監(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 () {
//當(dāng)滾動(dòng)條的位置處于距頂部200像素以下時(shí),頂部導(dǎo)航出現(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實(shí)現(xiàn)的導(dǎo)航條切換可顯示隱藏
- 用jQuery實(shí)現(xiàn)一些導(dǎo)航條切換,顯示隱藏的實(shí)例代碼
- JQuery實(shí)現(xiàn)鼠標(biāo)滑過(guò)顯示導(dǎo)航下拉列表
- 基于jquery實(shí)現(xiàn)導(dǎo)航菜單高亮顯示(兩種方法)
- jquery實(shí)現(xiàn)的讓超出顯示范圍外的導(dǎo)航自動(dòng)固定屏幕最頂上
- 基于jquery實(shí)現(xiàn)頁(yè)面滾動(dòng)時(shí)頂部導(dǎo)航顯示隱藏
- jQuery實(shí)現(xiàn)左側(cè)導(dǎo)航模塊的顯示與隱藏效果
- jquery實(shí)現(xiàn)隱藏與顯示動(dòng)畫(huà)效果/輸入框字符動(dòng)態(tài)遞減/導(dǎo)航按鈕切換
- php+jQuery實(shí)現(xiàn)的三級(jí)導(dǎo)航欄下拉菜單顯示效果
- jQuery實(shí)現(xiàn)的導(dǎo)航條點(diǎn)擊后高亮顯示功能示例
相關(guān)文章
jquery插件開(kāi)發(fā)方法(初學(xué)者)
現(xiàn)在jquery是比較流行的組件了,大家可以通過(guò)擴(kuò)展插件的方法自定義功能,大家可以參考下面的方法制作自己的插件2012-02-02JQuery 遮罩層實(shí)現(xiàn)(mask)實(shí)現(xiàn)代碼
用過(guò)ExtJs的可能知道在ExtJs中集成了很多的UI元素可以很方便我們的使用。2010-01-01jQuery mobile轉(zhuǎn)換url地址及獲取url中目錄部分的方法
這篇文章主要介紹了jQuery mobile轉(zhuǎn)換url地址及獲取url中目錄部分的方法,轉(zhuǎn)換url地址本文中介紹的是將相對(duì)url轉(zhuǎn)化為絕對(duì)url,需要的朋友可以參考下2015-12-12運(yùn)用jQuery定時(shí)器的原理實(shí)現(xiàn)banner圖片切換
banner圖片切換效果,在一些企業(yè)網(wǎng)站上經(jīng)常會(huì)碰到,本文運(yùn)用jQuery定時(shí)器的原理實(shí)現(xiàn)banner圖片切換,感興趣的朋友可以學(xué)習(xí)下2014-10-10淺談jQuery頁(yè)面的滾動(dòng)位置scrollTop、scrollLeft
官方文檔的解釋有點(diǎn)含糊,其實(shí)換個(gè)角度就很容易理解了,scrollTop獲取的是內(nèi)部元素超出外部容器的高度。 例如:$('window').scrollTop()獲取的就是當(dāng)前這個(gè)頁(yè)面超出窗口最上端的高度,scrollLeft與此同理2015-05-05jQuery實(shí)現(xiàn)字符串全部替換的方法【推薦】
本文主要介紹了jQuery實(shí)現(xiàn)字符串全部替換的方法,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03漢化英文版的Dreamweaver CS5并自動(dòng)提示jquery
如果從Adobe Dreamweaver CS5的官網(wǎng)上下載了一個(gè)Adobe Dreamweaver CS5,那么Adobe Dreamweaver CS5默認(rèn)你是英文版的,我們中國(guó)人還是比較習(xí)慣看漢字的。2010-11-11