English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article shares the specific code for the js draggable slider effect for your reference, the specific content is as follows
Effect:
Code:
!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <title>Mouse drag small block</title> <style type="text/css"> .lineDiv { position: relative; height: 5px; background: red; width: 300px; margin: 50px auto; } .lineDiv .minDiv { position: absolute; top: -5px; left: 0; width: 15px; height: 15px; background: green; cursor: pointer } .lineDiv .minDiv .vals { position: absolute; font-size: 20px; top: -45px; left: -10px; width: 35px; height: 35px; line-height: 35px; text-align: center; background: blue; } .lineDiv .minDiv .vals:after { content: ""; width: 0px; height: 0px; border-top: 6px solid blue; border-left: 6px solid transparent; border-right: 6px solid transparent; border-bottom: 6px solid transparent; display: block; margin-left: 11px; } </style> </head> <body> <center> <h3>use the mouse to drag the small block<span id="msg">0</span>%</h3> </center> <div id="lineDiv" class="lineDiv"> <div id="minDiv" class="minDiv"> <div id="vals" class="vals">0</div> </div> </div> <script> window.onload = function() { var lineDiv = document.getElementById('lineDiv'); //long line var minDiv = document.getElementById('minDiv'); //small block var msg = document.getElementById("msg"); var vals = document.getElementById("vals"); var ifBool = false; //determine if the mouse is pressed //mouse press on the small block minDiv.addEventListener("touchstart", function(e) { e.stopPropagation(); ifBool = true; console.log("mouse press") }); //drag window.addEventListener("touchmove", function(e) { console.log("mouse drag") if(ifBool) { var x = e.touches[0].pageX || e.touches[0].clientX; //mouse horizontal coordinate var x var lineDiv_left = getPosition(lineDiv).left; //the horizontal coordinate of the long line var minDiv_left = x - lineDiv_left; //the left value of the small block relative to the parent element (long line) if(minDiv_left >= lineDiv.offsetWidth - 15) {}} minDiv_left = lineDiv.offsetWidth - 15; } if(minDiv_left < 0) { minDiv_left = 0; } //Set the left value of the small block after dragging minDiv.style.left = minDiv_left + "px"; msg.innerText = parseInt((minDiv_left / (lineDiv.offsetWidth - 15)) * 100); vals.innerText = parseInt((minDiv_left / (lineDiv.offsetWidth - 15)) * 100); } }); //Mouse release window.addEventListener("touchend", function(e) { console.log("Mouse up") ifBool = false; }); //Get the absolute position of the element function getPosition(node) { var left = node.offsetLeft; //Get the left value of the element relative to its parent element var left var top = node.offsetTop; current = node.offsetParent; // Get the offsetParent of the element // Keep looping until the root element while(current != null) { left += current.offsetLeft; top += current.offsetTop; current = current.offsetParent; } return { "left": left, "top": top }; } } </script> </body> </html>
That is all for this article. I hope it will be helpful for your learning, and I also hope everyone will support the Yelling Tutorial more.