這篇文章主要為大家展示了JS如何實現簡單移動端鼠標拖拽,內容簡而易懂,希望大家可以學習一下,學習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
#div {
width: 100%;
height: 200px;
background: rosybrown;
}
#button {
position: absolute;
}
</style>
</head>
<body>
<div id="div">
<button id="button">看我的魔法屌不屌</button>
</div>
<script>
var button = document.getElementById('button')
button.ontouchstart = function(e) {
var startX = e.touches[0].clientX - this.offsetLeft;
var startY = e.touches[0].clientY - this.offsetTop;
this.ontouchmove = function(e) {
button.style.left = e.touches[0].clientX - startX + 'px';
button.style.top = e.touches[0].clientY - startY + 'px';
}
}
button.ontouchend = function() {
button.ontouchmove = null;
}
</script>
</body>
</html>以上就是關于JS如何實現簡單移動端鼠標拖拽的內容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。