您好,登錄后才能下訂單哦!
小編給大家分享一下如何使用原生js實現可兼容PC和移動端的拖動滑塊功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體如下:
在PC端可以用mousedown來觸發一個滑塊滑動的效果,但在手機上,貌似無法識別這個事件,但手機上有touchstart事件,可以通過一系列“touch”事件來替代PC端的“mouse”事件。
移動端觸屏滑動的效果其實就是圖片輪播,在PC的頁面上很好實現,綁定click和mouseover等事件來完成。但是在移動設備上,要實現這種輪播的效果,就需要用到核心的touch事件。處理touch事件能跟蹤到屏幕滑動的每根手指。
以下是四種touch事件
touchstart: //手指放到屏幕上時觸發
touchmove: //手指在屏幕上滑動式觸發
touchend: //手指離開屏幕時觸發
touchcancel: //系統取消touch事件的時候觸發,這個好像比較少用
每個觸摸事件被觸發后,會生成一個event對象,event對象里額外包括以下三個觸摸列表
touches: //當前屏幕上所有手指的列表
targetTouches: //當前dom元素上手指的列表,盡量使用這個代替touches
changedTouches: //涉及當前事件的手指的列表,盡量使用這個代替touches
這些列表里的每次觸摸由touch對象組成,touch對象里包含著觸摸信息,主要屬性如下:
clientX / clientY: //觸摸點相對瀏覽器窗口的位置
pageX / pageY: //觸摸點相對于頁面的位置
screenX / screenY: //觸摸點相對于屏幕的位置
identifier: //touch對象的ID
target: //當前的DOM元素
<!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>鼠標拖動小方塊</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; //判斷鼠標是否按下 //鼠標按下方塊 minDiv.addEventListener("touchstart", function(e) { e.stopPropagation(); ifBool = true; console.log("鼠標按下") }); //拖動 window.addEventListener("touchmove", function(e) { console.log("鼠標拖動") if(ifBool) { var x = e.touches[0].pageX || e.touches[0].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); } }); //鼠標松開 window.addEventListener("touchend", function(e) { console.log("鼠標彈起") ifBool = false; }); //獲取元素的絕對位置 function getPosition(node) { var left = node.offsetLeft; //獲取元素相對于其父元素的left值var left var top = node.offsetTop; current = node.offsetParent; // 取得元素的offsetParent // 一直循環直到根元素 while(current != null) { left += current.offsetLeft; top += current.offsetTop; current = current.offsetParent; } return { "left": left, "top": top }; } } </script> </body> </html>
兼容PC端和移動端:
<!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>鼠標拖動小方塊</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 // 一直循環直到根元素 while(current != null) { left += current.offsetLeft; top += current.offsetTop; current = current.offsetParent; } return { "left": left, "top": top }; } } </script> </body> </html>
以上是“如何使用原生js實現可兼容PC和移動端的拖動滑塊功能”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。