首页 > 代码库 > 手机版 div拖动

手机版 div拖动

<!doctype html>
<html> 
<head> 
<title></title> 
<script type="text/javascript"> 
var mouseX; 
var objX; 
var isDowm = false; //是否按下鼠标 
function mouseDown(obj, e) { 
obj.style.cursor = "move"; 

mouseX = e.clientX;
isDowm = true; 
} 
function mouseMove(e) { 
var div = document.getElementById("div1"); 
var x = e.clientX;
if (isDowm) { 
div.style.left = parseInt(objX) + parseInt(x) - parseInt(mouseX) + "px"; 
} 
} 
function mouseUp(e) { 
if (isDowm) { 
var x = e.clientX;
var div = document.getElementById("div1"); 
div.style.left = (parseInt(x) - parseInt(mouseX) + parseInt(objX)) + "px";
mouseX = x;
div1.style.cursor = "default";
isDowm = false; 
} 
} 
</script> 
</head> 
<body> 
<div id="div1" style="background-color: Green; border: 1px solid red; height: 300px; 
top: 100px; left: 100px; width: 300px; position: absolute;" onm ousedown="mouseDown(this,event)" 
onmousemove="mouseMove(event)" onm ouseup="mouseUp(event)"> 
</div> 
</body> 
</html>

 

手机版 div拖动