Recently, when I was doing a practice project, I needed to use a slider, so I did some research on it. First, let’s look at the horizontal slider. The effect is as follows: The code is as follows: <html> <head> <meta charset="UTF-8"> <title>Horizontal Slider</title> <style> * { margin: 0; padding: 0; } .scroll { margin: 100px; width: 500px; height: 5px; background: #ccc; position: relative; } .bar { width: 10px; height: 20px; background: #369; position: absolute; top: -7px; left: 0; cursor: pointer; } p{ margin-left: 100px; } </style> </head> <body> <div class="scroll" id="scroll"> <div class="bar" id="bar"> </div> </div> <p></p> <script> //Get the element var scroll = document.getElementById('scroll'); var bar = document.getElementById('bar'); var ptxt = document.getElementsByTagName('p')[0]; bar.onmousedown = function(event) { var event = event || window.event; // X of the page event minus the current positioned element relative to the nearest ancestor var x = event.clientX - this.offsetLeft; document.onmousemove = function(event) { var event = event || window.event; var left = event.clientX - x; if (left < 0) left = 0; else if (left > scroll.offsetWidth - bar.offsetWidth) { left = scroll.offsetWidth - bar.offsetWidth; } //Change the left position of the slider bar.style.left = left + "px"; ptxt.innerHTML = "The current slider's movement percentage: " + parseInt(left / (scroll.offsetWidth - bar.offsetWidth) * 100) + "%"; //Prevent selection window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); } } //When the mouse pops up, do nothing document.onmouseup = function() { document.onmousemove = null; } </script> </body> </html> The vertical slider effect is as follows: The code is as follows: <html> <head> <meta charset="UTF-8"> <title>Vertical Slide Bar</title> <style> * { margin: 0; padding: 0; } .scroll{ margin: 100px; width: 5px; height: 320px; background: #ccc; position: relative; } .bar { width: 15px; height: 5px; background: #369; position: absolute; top: 0px; left: -5; cursor: pointer; } p{ margin-left: 100px; } </style> </head> <body> <div class="scroll" id="scroll"> <div class="bar" id="bar"> </div> </div> <p></p> <script> //Get the element var scroll = document.getElementById("scroll"); var bar = document.getElementById("bar"); var ptxt = document.getElementsByTagName('p')[0]; //Add event bar.onmousedown = function(event) { var event = event || window.event; //The Y of the page event minus the current positioned element relative to the nearest ancestor var y = event.clientY - this.offsetTop; //Drag needs to be written to down document.onmousemove = function(event) { var event = event || window.event; //Get the moving distance var top = event.clientY - y; if (top < 0){ top = 0; } else if (top > scroll.offsetHeight - bar.offsetHeight){ top = scroll.offsetHeight - bar.offsetHeight; } //Change the top of the slider bar.style.top = top + "px"; //Get the current sliding distance according to the percentage ptxt.innerHTML = "The current slider's movement percentage: " + parseInt(top/(scroll.offsetHeight - bar.offsetHeight) * 100) + "%"; //Prevent selection window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); } } //When the mouse pops up, do nothing document.onmouseup = function() { document.onmousemove = null; } </script> </body> </html> The reason why the moving percentage display effect is added here is mainly to achieve the purpose of dynamic control if it is necessary to connect to the background data in the future. 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:
|
<<: Summary of knowledge points about null in MySQL database
>>: Difference and principle analysis of Nginx forward and reverse proxy
Introduction Describes the use cases and solution...
DOCTYPE Doctype is used to tell the browser which...
Let's take a look at the code first <form ...
Rendering If you want to achieve the effect shown...
Preface Everyone knows that the partition field m...
Table of contents Preface What does yarn create d...
Table of contents design Component Communication ...
This article uses an example to describe the MySQ...
This article shares the specific code of vue+elem...
Table of contents 1. Problem Description 2. Cause...
Sometimes the input box is small, and you want to...
Table of contents Global variable globalData Page...
Table of contents 1. Objectives 2. Environmental ...
This article shares the specific code for JavaScr...
1. Command method Run the nginx service in the cr...