jQuery中scrollTop()方法用法實(shí)例
本文實(shí)例講述了jQuery中scrollTop()方法用法。分享給大家供大家參考。具體分析如下:
此方法返回或設(shè)置匹配元素的滾動條的垂直偏移量。
語法結(jié)構(gòu)一:
當(dāng)scrollTop()方法不帶有參數(shù)的時(shí)候是返回匹配元素相對滾動條頂部的偏移量。
實(shí)例代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>scrollTop()函數(shù)-腳本之家</title>
<style type="text/css">
#div1{
border:1px solid black;
width:200px;
height:200px;
overflow:auto
}
#div2{height:250px;}
#div3{
height:48px;
width:30px;
border:1px solid red;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
alert($("#div1").scrollTop()+" px");
});
});
</script>
</head>
<body>
<div id="div1">
<div id="div2">
<div id="div3"></div>
</div>
</div>
<button>獲得offset值</button>
</body>
</html>
語法結(jié)構(gòu)二:
當(dāng)scrollTop()方法帶有參數(shù)的時(shí)候置垂直滾動條頂部偏移為該值。
參數(shù)列表:
實(shí)例代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.dbjr.com.cn/" />
<title>scrollTop()函數(shù)-腳本之家</title>
<style type="text/css">
#div1{
border:1px solid black;
width:200px;
height:200px;
overflow:auto
}
#div2{height:250px;}
#div3{
height:48px;
width:30px;
border:1px solid red;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("#div1").scrollTop(30);
})
})
</script>
</head>
<body>
<div id="div1">
<div id="div2">
<div id="div3"></div>
</div>
</div>
<button>設(shè)置offset值</button>
</body>
</html>
希望本文所述對大家的jquery程序設(shè)計(jì)有所幫助。
相關(guān)文章
jQuery實(shí)現(xiàn)表格行數(shù)據(jù)滾動效果
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)表格行數(shù)據(jù)滾動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08實(shí)例詳解jQuery Mockjax 插件模擬 Ajax 請求
這篇文章主要介紹了實(shí)例詳解jQuery Mockjax 插件模擬 Ajax 請求的相關(guān)資料,需要的朋友可以參考下2016-01-01jQuery實(shí)現(xiàn)聯(lián)動下拉列表查詢框
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)聯(lián)動下拉列表查詢框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01jquery的trigger和triggerHandler的區(qū)別示例介紹
這篇文章主要介紹了jquery的trigger和triggerHandler的區(qū)別,需要的朋友可以參考下2014-04-04Jquery使用each函數(shù)實(shí)現(xiàn)遍歷及數(shù)組處理
這篇文章主要介紹了Jquery使用each函數(shù)實(shí)現(xiàn)遍歷及數(shù)組處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07jquery實(shí)現(xiàn)漫天雪花飛舞的圣誕祝福雪花效果代碼分享
這篇文章主要介紹了jquery實(shí)現(xiàn)漫天雪花飛舞的圣誕祝福雪花效果,很浪漫,感興趣的小伙伴們可以參考一下2015-08-08