1. 程式人生 > >pc端拖拽事件

pc端拖拽事件

asc on() tex 綁定 round html ext var meta

<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>

pc端拖拽事件