簡單javascript拖動效果。

來源:互聯網
上載者:User
Code
<!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">
<head>
    <title></title>
    <script type="text/javascript">
        function Drag(ele, target) {
            function start(e) {
                e = e || window.event;
                started = true;
                moved = this.target;
                initX = parseInt(moved.style.left);
                initY = parseInt(moved.style.top);
                offsetX = e.clientX;
                offsetY = e.clientY;
                document.onmousemove = function(event) {
                    if (started) {
                        event = event || window.event;
                        var x = event.clientX - offsetX + initX;
                        var y = event.clientY - offsetY + initY;
                        var doc = document.documentElement;  
                        //防止往左右移出,如不需要可去掉if
                        if (x > 0 && x + moved.offsetWidth < Math.max(doc.scrollWidth, doc.clientWidth))
                            moved.style.left = x + "px";
                        //防止上下移出,如不需要可去掉if
                        if (y > 0 && y + moved.offsetHeight < Math.max(doc.scrollHeight, doc.clientHeight))
                            moved.style.top = y + "px";
                    }
                };
                document.onmouseup = function() {
                    started = false;
                    document.onmousemove = null;
                }
            };
            ele.target = target;
            ele.onmousedown = start;
        }

        window.onload = function() {
            //用法,第一個參數為要用來拖動的元素,第二個參數為被拖動元素。
            new Drag(document.getElementById("head"), document.getElementById("box"));
            new Drag(document.getElementById("Div2"), document.getElementById("Div1"));
        }
    </script>
</head>
<body>   
    <div id="box" style="border:1px solid #ccc; top:20px; left:20px; background-color:#f0f0f0; width:300px; height:180px; position:absolute;">
        <div id="head" style="background-color:#ddd; height:26px; cursor:move;"></div>
    </div>
    <div id="Div1" style="border:1px solid #ccc; top:150px; left:150px; background-color:#f0f0f0; width:300px; height:180px; position:absolute;">
        <div id="Div2" style="background-color:#ddd; height:26px; cursor:move;"></div>
    </div>
    <div id="status" style="position:absolute; right:10px; top:10px;"></div>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.