This article shares the specific code of how to drag the slider in js for your reference. The specific content is as follows To realize dragging the slider, first analyze that the slider can be dragged, so the coordinates of the slider in the page should be changed. Then use positioning to get the top and left of the element and assign them values. The next step is to prepare events. Since it is mouse dragging, there should be three events: mousedown, mousemove, and mouseup. The slider is selected through the mousedown event, and the slider is dragged through the mousemove event. When dragging the slider, the coordinates of the mouse in the visible window are obtained and assigned to the top and left of the slider. Specific code implementation <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } div { width: 60px; height: 60px; background-color: coral; border-radius: 20%; position: absolute; border: 6px solid skyblue; left: 0; top: 0; } </style> </head> <body> <div></div> <script> let div = document.querySelector('div') let x, y let fn = function (e) { // console.log('hhhhhhhh') div.style.left = e.clientX - x + 'px' div.style.top = e.clientY - y + 'px' if (e.clientX - x < 30) { div.style.left = 0 } if (e.clientY - y < 30) { div.style.top = 0 } if (e.clientX - x > document.documentElement.clientWidth - div.offsetWidth - 30) { div.style.left = document.documentElement.clientWidth - div.offsetWidth + 'px' } if (e.clientY - y > document.documentElement.clientHeight - div.offsetHeight - 30) { div.style.top = document.documentElement.clientHeight - div.offsetHeight + 'px' } } div.addEventListener('mousedown', function (e) { x = e.offsetX y = e.offsetY document.addEventListener('mousemove', fn) }) div.addEventListener('mouseup', function () { document.removeEventListener('mousemove', fn) }) </script> </body> </html> run 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:
|
<<: Docker meets Intellij IDEA, Java development improves productivity tenfold
>>: Differences between MySQL CHAR and VARCHAR when storing and reading
Nginx (engine x) is a lightweight, high-performan...
The vue part is as follows: <template> <...
1. Use the df command to view the overall disk us...
0. What is a tag? XML/HTML CodeCopy content to cl...
Everyone is familiar with the meta tag in desktop...
This article is a self-written imitation of the X...
This article shares a draggable login box impleme...
Table of contents Why use Docker? Docker installa...
The reason is this I wanted to deploy a mocker pl...
<br />Looking at this title, you may find it...
Result: Implementation code: html <div class=&...
1. this.$router.push() 1. Vue <template> &l...
background When performing a SQL query, I tried t...
Generally speaking, we can have the following two...
Prepare 1. Download the required installation pac...