当前位置:网站首页>Using native JS to realize custom scroll bar (click to reach, drag to reach)

Using native JS to realize custom scroll bar (click to reach, drag to reach)

2022-07-26 10:26:00 Xaivor

1.HTML file

div1 Is a scroll bar ,div2 It's a rolling ball ,div3 Is a text area container ,div4 Is the text area .

<div id="div">
	<div id="div1">
		<div id="div2">	</div>
	</div>
	<div id="div3">
		<div id="div4">
			<p>CSS3  course </p>
			<p>CSS3  course </p>
			<p>CSS3  brief introduction </p>
			<p>CSS3  Frame </p>
			<p>CSS3  Round corners </p>
			<p>CSS3  background </p>
			<p>CSS3  The gradient </p>
			<p>CSS3  Text effect </p>
			<p>CSS3  typeface </p>
			<p>CSS3 2D  transformation </p>
			<p>CSS3 3D  transformation </p>
			<p>CSS3  transition </p>
			<p>CSS3  Animation </p>
			<p>CSS3  Multiple columns </p>
			<p>CSS3  The user interface </p>
			<p>CSS3  picture </p>
			<p>CSS3  Button </p>
			<p>CSS3  Pagination </p>
			<p>CSS3  Box size </p>
			<p>CSS3  Elastic box </p>
			<p>CSS3  Multimedia query </p>
			<p>CSS3  Multimedia query examples </p>
		</div>
	</div>
</div>

2.css The style file

Hidden by container overflow , Absolute positioning of text area , And then give it to js Handle .

*{
    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: " Microsoft YaHei ";
font-size: 19px; letter-spacing: 1px; padding: 3px 6px;}

3.js Script code

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');
	
	//  Progress bar drag , Content follows sports events 
	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;
	};
	
	//  Click on the progress bar , Turn on timer , The ball moves at a constant speed and arrives , Clear timer 
	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);
		
	}
	
	
	
}
原网站

版权声明
本文为[Xaivor]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207261014503907.html