欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js實現(xiàn)控制整個頁面滾動條的位置

 更新時間:2022年10月25日 15:58:59   作者:stefanie_sun723  
這篇文章主要介紹了js實現(xiàn)控制整個頁面滾動條的位置,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

js控制整個頁面滾動條位置

方法一

1、通過div的scrollTop變動控制垂直滾動條位置。通過div的scrollLeft變動控制水平滾動條位置。

示例:

<body>
//d1是外層div,帶滾動條
<div id='d1' style='height:200px;width:100px;overflow:auto;background:blue;'>
<div style='height:500px;width:500px;background:yellow'>2222</div>
</div>
</body>
<script>
document.getElementById('d1').scrollTop=100;//通過scrollTop設(shè)置滾動到100位置
document.getElementById('d1').scrollLeft=200;//通過scrollTop設(shè)置滾動到200位置
</script>

方法二

2、用html錨點

如下:

<a href="#test" rel="external nofollow" >aaaaa</a>
<div id="test">lalallalalalala</div>

js滾動條屬性、設(shè)置滾動條滾動速度

返回不帶px的數(shù)值,沒用負數(shù),最小為0

  • .scrollTop:豎直滾動條到頂部的距離,即瀏覽器視口外的高度
  • .scrollLeft:水平滾動條到最左邊的距離
  • .scrollHeight:滾動內(nèi)容區(qū)域的高度

返回整個窗口的滾動條數(shù)值

非ie        

返回整個窗口的滾動條數(shù)值

  • document.documentElement.scrollTop:豎直滾動條到頂部的距離
  • document.documentElement.scrollLeft:水平滾動條到最左邊的距離

ie和非ie獲得滾動條的兼容寫法      

var temp=document.documentElement.scrollTop||document.body.scrollTop

設(shè)置滾動條速度代碼示例

<html>
	<head>
		<meta charset="utf-8">
		<title>滾動事件</title>
		<style>
			div{
				height:2000px;
				
			}
			button{
				position:fixed;
				bottom: 100px;
				right: 100px;
			}
		</style>
	</head>
	<body>
		<div>
		<button class="btn">去那里</button>
	
	
		</div>
		
		<script>
			var button=document.querySelector("button");
			
			button.onclick=function(){
				var id=setInterval(function(){
					if(document.documentElement.scrollTop>0)
					{
						document.documentElement.scrollTop-=100;
						
						//當滑動條距頂部為0時,結(jié)束間隔任務(wù)
						if(document.documentElement.scrollTop==0)
						{
							clearInterval(id);
						}
					}
				},500);
				
			}
			
		</script>
	</body>
</html>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論