In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how js can achieve the effect of dragging the slider. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The details are as follows
To achieve dragging the slider, first analysis, the slider can be dragged should change the coordinates of the slider in the page, then use the location to get the elements of the top and left to assign them, and then is to prepare the event, since it is a mouse drag should have mousedown,mousemove,mouseup three events, through the mousedown mouse down event to select the slider, mousemove event to drag the slider Get the top and left assigned to the slider by mouse coordinates in the visual window when dragging the slider
Specific code implementation
Document * {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;} 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)})
Running
This is the end of the article on "how to drag the slider effect in js". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.