码迷,mamicode.com
首页 > 其他好文 > 详细

dom 拖拽div

时间:2015-01-16 18:44:31      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1{
    width:100px;
    height:100px;
    background:red;
    position:absolute;
    }
</style>
<script>
window.onload = function ()
{
    var odiv = document.getElementById(‘div1‘);
    
    odiv.onmousedown = function (ev)
    {
        var ev = ev || event;
        var dix = ev.clientX - this.offsetLeft;
        var diy = ev.clientY - this.offsetTop;
        document.onmousemove = function (ev)
        {
            var ev = ev || event;
            odiv.style.top = ev.clientY - diy + ‘px‘;
            odiv.style.left =ev.clientX - dix + ‘px‘; 
        }
        
        document.onmouseup = function ()
        {
            document.onmousedown = document.onmousemove = null;
        }
    }
}
</script>
</head>

<body>
<div id="div1"></div>    
</body>
</html>

 

dom 拖拽div

标签:

原文地址:http://www.cnblogs.com/mayufo/p/4229416.html

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