JS实现简单移动端鼠标拖拽
更新时间:2020年07月23日 15:29:47 作者:渴望 Long For
这篇文章主要为大家详细介绍了JS实现简单移动端鼠标拖拽,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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>
更多精彩文章请点击专题: Javascript拖拽特效汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
JavaScript中对象property的读取和写入方法介绍
这篇文章主要介绍了JavaScript中对象property的读取和写入方法介绍,本文讲解了原型继承链中property的读取、原型继承链中property的写入等内容,需要的朋友可以参考下2014-12-12
Js中parentNode,parentElement,childNodes,children之间的区别
这篇文章主要是对Js中parentNode,parentElement,childNodes,children的区别进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助2013-11-11
ArrayBuffer Uint8Array Blob与文本字符相互转换示例
这篇文章主要为大家介绍了ArrayBuffer Uint8Array Blob与文本字符相互转换示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-06-06


最新评论