<body>
<div style="width: 100px;height: 100px;background-color: red;position:absolute;left: 0;top: 0;"></div>
<script type="text/javascript">
var div = document.getElementsByTagName("div")[0];
var dix;
var diy;
document.onmousedown = function (e) {
dix=e.pageX-parseInt(div.style.left);
diy=e.pageY-parseInt(div.style.top);
div.onmousemove = function (e) {
var event = e || window.event;
div.style.left = e.pageX -dix + "px";
div.style.top = e.pageY -diy + "px";
}
document.onmouseup=function(){
div.onmousemove=null;
}
}
</script>
</body>
e.pageX、e.pageY是获取鼠标在网页上的位置
鼠标事件里的onmousedown有一个属性:
0表示鼠标左键点击
1表示滚轮点击
2表示鼠标左键点击