当前位置:网站首页>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);
}
}
边栏推荐
- 【Halcon视觉】图像的傅里叶变换
- 【Halcon视觉】阈值分割
- Function templates and non template functions with the same name cannot be overloaded (definition of overloads)
- Comparison of packet capturing tools fiddler and Wireshark
- Agenda express | list of sub forum agenda on July 27
- 30 minutes to thoroughly understand the synchronized lock upgrade process
- Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da
- 【Halcon视觉】软件编程思路
- SPARK中 DS V2 push down(下推)的一些说明
- Use of Android grendao database
猜你喜欢
The software cannot be opened

如何写一篇百万阅读量的文章
![[Halcon vision] morphological corrosion](/img/f9/f01dd3340824ff28c84cf7bb52882e.png)
[Halcon vision] morphological corrosion

【Halcon视觉】图像的傅里叶变换

Employee information management system based on Web

Data communication foundation telnet remote management equipment

On the compilation of student management system of C language course (simple version)

【Halcon视觉】仿射变换
软件打不开了

About automatic operation on Web pages
随机推荐
【Halcon视觉】图像滤波
Okaleido生态核心权益OKA,尽在聚变Mining模式
Deduct daily question 838 of a certain day
Data communication foundation telnet remote management equipment
The CLOB field cannot be converted when querying Damon database
[Halcon vision] software programming ideas
Learning about tensorflow (II)
上传图片获取宽高
码云,正式支持 Pages 功能,可以部署静态页面
[Halcon vision] affine transformation
Application of crosstab in SQL Server
SQL Server 2008 R2 installation problems
mysql 进不去了怎么办
Closure of go (cumulative sum)
Study on the basis of opencv
【Halcon视觉】图像的傅里叶变换
面试第一家公司的面试题及答案(一)
Self encapsulated database dbutils universal template
Some descriptions of DS V2 push down in spark
Using undertow, Nacos offline logout delay after service stop