1. 程式人生 > >利用原生js實現自定義滾動條(可點選到達,拖動到達)

利用原生js實現自定義滾動條(可點選到達,拖動到達)

1.HTML檔案

div1是滾動條,div2是滾動小球,div3是文字區域容器,div4是文字區域。

<div id="div">
	<div id="div1">
		<div id="div2">	</div>
	</div>
	<div id="div3">
		<div id="div4">
			<p>CSS3 教程</p>
			<p>CSS3 教程</p>
			<p>CSS3 簡介</p>
			<p>
CSS3 邊框</p> <p>CSS3 圓角</p> <p>CSS3 背景</p> <p>CSS3 漸變</p> <p>CSS3 文字效果</p> <p>CSS3 字型</p> <p>CSS3 2D 轉換</p> <p>CSS3 3D 轉換</p> <p>CSS3 過渡</p> <p>CSS3 動畫</p> <p>CSS3 多列</
p
>
<p>CSS3 使用者介面</p> <p>CSS3 圖片</p> <p>CSS3 按鈕</p> <p>CSS3 分頁</p> <p>CSS3 框大小</p> <p>CSS3 彈性盒子</p> <p>CSS3 多媒體查詢</p> <p>CSS3 多媒體查詢例項</p> </div> </div> </div>

2.css樣式檔案

通過容器溢位隱藏,文字區域的絕對定位,然後再交給js處理。

*{padding: 0; margin: 0;}
#div{top:200px;left:25%;width: 50%;height: 300px; position: absolute; 
}
#div1{width: 20px; height: 300px; position: relative; 
background: #CCCCCC; border-radius: 28px; float: right; cursor: pointer;}
#div1 #div2{left: -4px;width: 28px;height: 28px;border-radius: 50%; background: red;
position: absolute;}

#div3{width: 90%; height: 300px; border: 2px solid #CCCCCC;
position: relative; float: left; overflow: hidden;}
#div3 #div4{top:0;left:0;width: 100%; position: absolute; font-family: "微軟雅黑";
font-size: 19px; letter-spacing: 1px; padding: 3px 6px;}

3.js指令碼程式碼

window.onload =function(){
	var allDiv =document.getElementById('div');
	var oDiv =document.getElementById('div2');
	var aDiv =document.getElementById('div1');
	var textDiv1 =document.getElementById('div3');
	var textDiv2 =document.getElementById('div4');
	
	// 進度條拖動,內容跟著運動事件
	oDiv.onmousedown =function(ev){
		var oEvent =ev||event;
		
		var disY =oEvent.clientY -oDiv.offsetTop;
		
		if(oDiv.setCapture){
			oDiv.onmousemove =mouseMove;
			oDiv.onmouseup =mouseUp;
			
			oDiv.setCapture();
		}else{
			document.onmousemove =mouseMove;
			document.onmouseup =mouseUp;
		}
		
		function mouseMove(ev){
			var oEvent =ev||event;						
			var t =oEvent.clientY -disY;					
			var bottomLine = aDiv.offsetHeight-oDiv.offsetHeight;	
			
			
			if(t <0){
				t =0;
			}else if(t >bottomLine){
				t =bottomLine;
			}
			
	        var percent =t/272;
	        
			oDiv.style.top =t+'px';
			textDiv2.style.top =-(textDiv2.offsetHeight-textDiv1.offsetHeight)*percent+'px';
			
		};
		
		function mouseUp(){
			this.onmousemove =null;
			this.onmouseup =null;
			
			if(oDiv.releaseCapture){
				oDiv.releaseCapture();
			}
		};
	
	    return false;
	};
	
	// 點選進度條,開啟定時器,小球做勻速運動到達後,清楚定時器
	aDiv.onmousedown=function(ev){
		var oEvent =ev||event;					
		var divY =oEvent.clientY-allDiv.offsetTop;
		var timer =null;var speed=10;
		
		
		clearInterval(timer)
		timer = setInterval(function(){
			var percent=oDiv.offsetTop/272;
			
			
			if(oDiv.offsetTop<divY-28){
				oDiv.style.top =oDiv.offsetTop + speed +'px';
				textDiv2.style.top =-(textDiv2.offsetHeight-textDiv1.offsetHeight)*percent +'px';
			}else if(oDiv.offsetTop>divY){
				oDiv.style.top =oDiv.offsetTop - speed +'px';
				textDiv2.style.top =-(textDiv2.offsetHeight-textDiv1.offsetHeight)*percent +'px';
			}else if(oDiv.offsetTop>260){
				oDiv.offsetTop = 272+'px';
				clearInterval(timer);
			}else if(oDiv.offsetTop<10){
				oDiv.offsetTop = 0+'px';
				clearInterval(timer);
			}else{
				clearInterval(timer);
			}
			
			
		},10);
		
	}
	
	
	
}