zoukankan      html  css  js  c++  java
  • dom 拖拽div

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>无标题文档</title>
    <style>
    #div1{
        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>
  • 相关阅读:
    学习进度条
    学术诚信与职业道德
    czxt
    操作系统
    04 17评论博客
    0414 结对 2.0 33 34
    0408 汉堡包
    (补)结对心得
    构建之法4读后感
    复利计算4.0
  • 原文地址:https://www.cnblogs.com/mayufo/p/4229416.html
Copyright © 2011-2022 走看看