这篇文章主要为大家展示了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如何实现简单移动端鼠标拖拽的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。