码迷,mamicode.com
首页 > 编程语言 > 详细

javascript 拖拽

时间:2017-04-28 18:25:46      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:meta   add   doc   off   null   pagex   sed   get   clientx   

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>拖拽</title>
<style>
*{ margin: 0; padding: 0; }
#div1{ width: 100px; height: 100px; background-color: #669; position: absolute; left: 20px; top: 20px; cursor: move; }
#div2{ width: 100px; height: 100px; background-color: #996; position: absolute; left: 200px; top: 200px; cursor: move; }
</style>
</head>
<body>
<div id="div1">拖动的时候不会选中我</div>
<div id="div2">拖动的时候不会选中我</div>
<script>
var page = {
oEvent: function(ev){
return ev || event;
},
pageX: function(ev){
var e = this.oEvent(ev);
return e.clientX;
},
pageY: function(ev){
var e = this.oEvent(ev);
return e.clientY;
},
disX: function(ev){
var e = this.oEvent(ev);
return e.layerX || e.offsetX;
},
disY: function(ev){
var e = this.oEvent(ev);
return e.layerY || e.offsetY;
}
}

drap("div1", page);
drap("div2", page);

function drap(id,option){
var $$ = function(id){
return document.getElementById(id);
}
$$(id).onmousedown = function(ev){
var disX = option.disX();
var disY = option.disY();
document.onmousemove = function(ev){
var left = option.pageX() - disX;
var top = option.pageY() - disY;
$$(id).style.left = left + "px";
$$(id).style.top = top + "px";
}
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
}

return false;
}
}
</script>
</body>
</html>

javascript 拖拽

标签:meta   add   doc   off   null   pagex   sed   get   clientx   

原文地址:http://www.cnblogs.com/wzh1995/p/6782379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!