<!DOCTYPE>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
<meta name="format-detection" content="telephone=no"/>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="white">
<style type="text/css">
* { padding:0; margin:0;}
#div1 { width:100px; height:100px; background-color:Red; position:absolute;}
</style>
<script language="javascript" type="text/javascript">
function PhoneDrag(id) {
var _this = this;
this.oDiv = document.getElementById(id);
this.disX = 0;
this.disY = 0;
this.oDiv.addEventListener("touchstart", function (ev) {
_this.fnDown(ev);
}, false);
}
PhoneDrag.prototype.fnDown = function (ev) {
var _this = this;
var oEvent = ev.touches[0];
ev.preventDefault();
this.disX = oEvent.clientX - this.oDiv.offsetLeft;
this.disY = oEvent.clientY - this.oDiv.offsetTop;
document.addEventListener("touchmove", function (ev) {
_this.fnMove(ev);
}, false);
document.addEventListener("touchend", function (ev) {
//这里是手指抬起的时候 如何删除touchmove事件
document.removeEventListerner(document, "这里怎么填");
}, false);
}
PhoneDrag.prototype.fnMove = function (ev) {
var oEvent = ev.touches[0];
var iX = oEvent.clientX - this.disX;
var iY = oEvent.clientY - this.disY;
this.oDiv.style.top = iY + "px";
this.oDiv.style.left = iX + "px";
}
window.onload = function () {
new PhoneDrag("div1");
}
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
这是移动端移动DIV的例子
POPMUISE
慕码人8056858