本文為大家分享了js實現(xiàn)移動端滑塊拖動選擇數(shù)字的效果,可以通過鼠標拖拽滑動效果來選擇數(shù)字,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)公司是專業(yè)的隴縣網(wǎng)站建設公司,隴縣接單;提供網(wǎng)站建設、網(wǎng)站設計,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行隴縣網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <title>鼠標拖動小方塊</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>用鼠標拖動小方塊<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; //判斷鼠標是否按下 //事件 var start = function(e) { e.stopPropagation(); ifBool = true; console.log("鼠標按下") } var move = function(e) { console.log("鼠標拖動") if(ifBool) { if(!e.touches) { //兼容移動端 var x = e.clientX; } else { //兼容PC端 var x = e.touches[0].pageX; } //var x = e.touches[0].pageX || e.clientX; //鼠標橫坐標var x var lineDiv_left = getPosition(lineDiv).left; //長線條的橫坐標 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; } //設置拖動后小方塊的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); } } var end = function(e) { console.log("鼠標彈起") ifBool = false; } //鼠標按下方塊 minDiv.addEventListener("touchstart", start); minDiv.addEventListener("mousedown", start); //拖動 window.addEventListener("touchmove", move); window.addEventListener("mousemove", move); //鼠標松開 window.addEventListener("touchend", end); window.addEventListener("mouseup", end); //獲取元素的絕對位置 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)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享題目:js實現(xiàn)兼容PC端和移動端滑塊拖動選擇數(shù)字效果
鏈接地址:http://jinyejixie.com/article48/psphep.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計、商城網(wǎng)站、電子商務、企業(yè)建站、網(wǎng)站改版、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)