Implementing custom scroll bar with native js

Implementing custom scroll bar with native js

This article example shares the specific code of js to implement custom scroll bar for your reference. The specific content is as follows

1. HTML file

Div1 is the scroll bar, div2 is the scroll ball, div3 is the text area container, and div4 is the text area.

<div id="div">
 <div id="div1">
 <div id="div2"> </div>
 </div>
 <div id="div3">
 <div id="div4">
 <p>CSS3 Tutorial</p>
 <p>CSS3 Tutorial</p>
 <p>Introduction to CSS3</p>
 <p>CSS3 border</p>
 <p>CSS3 rounded corners</p>
 <p>CSS3 background</p>
 <p>CSS3 Gradients</p>
 <p>CSS3 text effects</p>
 <p>CSS3 fonts</p>
 <p>CSS3 2D Transformations</p>
 CSS3 3D Transformations
 <p>CSS3 Transitions</p>
 <p>CSS3 Animation</p>
 <p>CSS3 Multiple Columns</p>
 <p>CSS3 User Interface</p>
 <p>CSS3 images</p>
 <p>CSS3 Button</p>
 <p>CSS3 Pagination</p>
 <p>CSS3 box size</p>
 <p>CSS3 Flexible Box</p>
 <p>CSS3 Multimedia Queries</p>
 <p>CSS3 multimedia query example</p>
 </div>
 </div>
</div>

2.css style file

The container is hidden by overflow, the text area is absolutely positioned, and then handed over to js for processing.

*{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');
 
 // The progress bar is dragged, and the content follows the motion event 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 the progress bar to start the timer. When the ball reaches the target at a constant speed, clear the 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);
 
 }
 
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Native js implements custom scroll bar component
  • js realizes automatic scrolling of the scroll bar
  • Vue.js desktop custom scroll bar component beautification scroll bar VScroll
  • JS custom scroll bar effect
  • JavaScript implements a simple chat dialog box (with scroll bar)
  • Implementation of js custom scroll bar using js wheel event
  • JS implements scroll bar bottoming out to load more
  • Using js to implement a simple scroll bar process analysis
  • layer.js open example of hiding scroll bar
  • Vue pure js monitors the example of the scroll bar to the bottom
  • JavaScript gets the scroll bar position and slides the page to the anchor point

<<:  Solution to MySQL remote connection failure

>>:  Setting up shadowsocks+polipo global proxy in Linux environment

Recommend

Detailed Example of JavaScript Array Methods

Table of contents Introduction Creating an Array ...

Using better-scroll component in Vue to realize horizontal scrolling function

About Recently, in the process of learning Vue, I...

Key features of InnoDB - insert cache, write twice, adaptive hash index details

The key features of the InnoDB storage engine inc...

Javascript uses the integrity attribute for security verification

Table of contents 1. Import files using script ta...

ES6 loop and iterable object examples

This article will examine the ES6 for ... of loop...

Implementation of Nginx operation response header information

Prerequisite: You need to compile the ngx_http_he...

A brief analysis of the configuration items of the Angular CLI release path

Preface Project release always requires packaging...

JavaScript two pictures to understand the prototype chain

Table of contents 1. Prototype Relationship 2. Pr...

Detailed explanation of the basic usage of the img image tag in HTML/XHTML

The image tag is used to display an image in a we...

Solution to the docker command exception "permission denied"

In Linux system, newly install docker and enter t...

Linux automatic login example explanation

There are many scripts on the Internet that use e...

Hbase Getting Started

1. HBase Overview 1.1 What is HBase HBase is a No...

Analysis of the implementation process of three modes of VMWare network adapter

Three modes Bridged (bridge mode), NAT (network a...

Minimalistic website design examples

Web Application Class 1. DownForEveryoneOrJustMe ...