這篇文章主要介紹了jQuery中scrollTop()方法用法,實例分析了scrollTop()方法的功能、定義及返回或設置匹配元素的滾動條的垂直偏移量時的使用技巧,需要的朋友可以參考下
本文實例講述了jQuery中scrollTop()方法用法。分享給大家供大家參考。具體分析如下:
此方法返回或設置匹配元素的滾動條的垂直偏移量。
語法結構一:
當scrollTop()方法不帶有參數的時候是返回匹配元素相對滾動條頂部的偏移量。
復制代碼代碼如下:
$(selector).scrollTop()
實例代碼:
復制代碼代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.survivalescaperooms.com/" />
<title>scrollTop()函數-武林網</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>
語法結構二:
當scrollTop()方法帶有參數的時候置垂直滾動條頂部偏移為該值。
復制代碼代碼如下:
$(selector).scrollTop(val)
參數列表:
實例代碼:
復制代碼代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.survivalescaperooms.com/" />
<title>scrollTop()函數-武林網</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>設置offset值</button>
</body>
</html>
希望本文所述對大家的jquery程序設計有所幫助。