本文實(shí)例為大家分享了js拖動(dòng)滑塊效果的具體代碼,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)南開,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
效果:
代碼:
<!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>鼠標(biāo)拖動(dòng)小方塊</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> <h4>用鼠標(biāo)拖動(dòng)小方塊<span id="msg">0</span>%</h4> </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'); //長線條 var minDiv = document.getElementById('minDiv'); //小方塊 var msg = document.getElementById("msg"); var vals = document.getElementById("vals"); var ifBool = false; //判斷鼠標(biāo)是否按下 //鼠標(biāo)按下方塊 minDiv.addEventListener("touchstart", function(e) { e.stopPropagation(); ifBool = true; console.log("鼠標(biāo)按下") }); //拖動(dòng) window.addEventListener("touchmove", function(e) { console.log("鼠標(biāo)拖動(dòng)") if(ifBool) { var x = e.touches[0].pageX || e.touches[0].clientX; //鼠標(biāo)橫坐標(biāo)var x var lineDiv_left = getPosition(lineDiv).left; //長線條的橫坐標(biāo) var minDiv_left = x - lineDiv_left; //小方塊相對于父元素(長線條)的left值 if(minDiv_left >= lineDiv.offsetWidth - 15) { minDiv_left = lineDiv.offsetWidth - 15; } if(minDiv_left < 0) { minDiv_left = 0; } //設(shè)置拖動(dòng)后小方塊的left值 minDiv.style.left = minDiv_left + "px"; msg.innerText = parseInt((minDiv_left / (lineDiv.offsetWidth - 15)) * 100); vals.innerText = parseInt((minDiv_left / (lineDiv.offsetWidth - 15)) * 100); } }); //鼠標(biāo)松開 window.addEventListener("touchend", function(e) { console.log("鼠標(biāo)彈起") ifBool = false; }); //獲取元素的絕對位置 function getPosition(node) { var left = node.offsetLeft; //獲取元素相對于其父元素的left值var left var top = node.offsetTop; current = node.offsetParent; // 取得元素的offsetParent // 一直循環(huán)直到根元素 while(current != null) { left += current.offsetLeft; top += current.offsetTop; current = current.offsetParent; } return { "left": left, "top": top }; } } </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站標(biāo)題:基于JavaScript實(shí)現(xiàn)拖動(dòng)滑塊效果
分享URL:http://www.rwnh.cn/article42/ihjjec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、電子商務(wù)、動(dòng)態(tài)網(wǎng)站、網(wǎng)站策劃、網(wǎng)站營銷、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)