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
Duplicate form submission is the most common and ...
Today I happened to be helping a friend move his ...
<br />Structure and hierarchy reduce complex...
Table of contents 1. Overview 2. Use docker to de...
Promise is a new solution for asynchronous progra...
Recently, there is a particularly abnormal busine...
Case Description: - Use tables to achieve page ef...
Table of contents Preface 1. The effect is as sho...
This is an effect created purely using CSS. To pu...
By chance, I discovered that a SQL statement prod...
The reason is simple: In HTML documents, multiple ...
1 Overview System centos8, use httpd to build a l...
constraint Constraints ensure data integrity and ...
The inline-block property value becomes very usef...
Table of contents A brief overview of the replica...