标签:null ack var top sem offsetx bsp 取消 position
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<style type="text/css">
#box{
width: 208px;
height: 208px;
background: url(girl.gif) no-repeat;
position: absolute;
}
</style>
</head>
<body>
<div id="box"></div>
<script type="text/javascript">
//拖拽 mousedown mousemove mouseup
var oDiv = document.getElementById("box");
//1.给目标元素添加mousedown事件
oDiv.onmousedown = function(event){
var evt = event||window.event;
var offX = evt.offsetX;
var offY = evt.offsetY;
//2.给doucument绑定mousemove事件
document.onmousemove = function(event){
var evt = event||window.event;
oDiv.style.left = evt.pageX-offX+"px";
oDiv.style.top = evt.pageY -offY+ "px";
var point = {
x:(evt.pageX-offX),
y:(evt.pageY -offY)
}
}
}
// 3给document绑定mouseup事件
document.onmouseup = function(){
//取消事件
document.onmousemove = null;
}
</script>
</body>
</html>
标签:null ack var top sem offsetx bsp 取消 position
原文地址:http://www.cnblogs.com/zhangjunming/p/7252703.html