拖拽小例子

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<style type="text/css">
#div{width:100px;height: 100px;background: red;position: absolute;}
</style>
<script type="text/javascript">
window.onload=function(){

var odiv=document.getElementById("div");
odiv.onmousedown=function(ev){
var ev=ev||event;
var disX=ev.clientX-this.offsetLeft;
var disY=ev.clientY-this.offsetTop;
//捕获全局
if(odiv.setCapture){
odiv.setCapture();
}
document.onmousemove=function(ev){
var ev=ev||event;
odiv.style.left=ev.clientX-disX+"px";
odiv.style.top=ev.clientY-disY+"px";
}
document.onmouseup=function(){
document.onmousemove=document.onmouseup=null;
//释放全局捕获
if(odiv.releaseCapture){
odiv.releaseCapture();
}
}
return false;//阻止默认行为。
}
}
</script>
</head>
<body>
<p>这是段文字文字设计院</p>
<div id="div"></div>
</body>
</html>

posted @ 2017-11-08 14:23  hwmbh  阅读(99)  评论(0编辑  收藏  举报