当前位置:网站首页>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);
}
}
边栏推荐
- Cause: couldn‘t make a guess for 解决方法
- 【Halcon视觉】数组
- Self encapsulated database dbutils universal template
- 数据库的复习--1.概述
- The CLOB field cannot be converted when querying Damon database
- Learning about opencv (1)
- Cause: could't make a guess for solution
- [Halcon vision] affine transformation
- Structure of [Halcon vision] operator
- The charm of SQL optimization! From 30248s to 0.001s
猜你喜欢

Common errors when starting projects in uniapp ---appid

新建福厦铁路全线贯通 这将给福建沿海带来什么?

Some cutting-edge research work sharing of SAP ABAP NetWeaver containerization

2022/07/25 ------ arrangement of strings

Jpg to EPS

Cause: could't make a guess for solution
![[Halcon vision] image filtering](/img/4b/e73a8d589b49276d96621f0ef02449.png)
[Halcon vision] image filtering

3.1 leetcode daily question 6

js翻页、kkpager.js翻页

如何写一篇百万阅读量的文章
随机推荐
【Halcon视觉】图像滤波
Learning about tensorflow (II)
How to write a million reading article
modelsim 安装教程(应用未安装)
【杂谈】Error loading psycopg2 module :No module named psycopg2
Error in render: "typeerror: cannot read properties of undefined (reading 'length')" --- error when calling interface
Reproduce the snake game in C language (I) build pages and construct snakes
抓包工具fiddler和wireshark对比
Review of database -- 3. SQL language
The difference between equals and = =
Listening freely, the next stop of online text traffic competition?
Common errors when starting projects in uniapp ---appid
PTA class a 1001
Redis realizes the correct posture of token bucket
【Halcon视觉】仿射变换
议程速递 | 7月27日分论坛议程一览
All codes of Tetris
Okaleido生态核心权益OKA,尽在聚变Mining模式
【Halcon视觉】算子的结构
[Halcon vision] image filtering