Div拖動/調整大小實例


出處:http://blog.163.com/xhy_cy/blog/static/10497623201171254719630/

 

 

<html>

<head>

<title>Div拖動/調整大小實例</title>

</head>

<script type="text/javascript">

    //保留的位置

    var saveLeft, saveTop, saveWidth, saveHeight;

    var theBody;

    var eventType;     //事件種類, "move""resize"

    var div;

 

    //創建並設定div的參數

    function setDiv()

    {

        //防止重復打開

        if (div)

        {

            return;

        }

        var newLeft, newTop, newWidth, newHeight;

        theBody = document.body;

 

        div = document.createElement("div");

        div.id = "panelDiv";

        div.style.position = "absolute";

        div.style.backgroundColor = "#E5E5E5"

        div.style.padding = "2px 5px 5px 2px";

        div.style.overflow = "hidden";

        div.style.zIndex = 1;

 

        //設定打開的大小和位置

        Function()

        {

            var openType = document.getElementById("radio1").checked ? 0 : 1;

            if (openType == 0)   //默認大小默認位置居中打開

            {

                newWidth = "300px";

                newHeight = "300px";

                newLeft = (theBody.clientWidth - parseInt(newWidth)) / 2 + "px";

                newTop = (theBody.clientHeight - parseInt(newHeight)) / 2 + "px";

            }

            else //存儲的位置與大小

            {

                newWidth = saveWidth ? saveWidth : "300px";

                newHeight = saveHeight ? saveHeight : "300px";

                newLeft = saveLeft ? saveLeft : (theBody.clientWidth - parseInt(newWidth)) / 2 + "px";

                newTop = saveTop ? saveTop : (theBody.clientHeight - parseInt(newHeight)) / 2 + "px";

            }

            div.style.width = newWidth;

            div.style.height = newHeight;

            div.style.left = newLeft;

            div.style.top = newTop;

        }

        div = setChild(div);

        theBody.appendChild(div);

 

        var ipt = document.getElementsByTagName("input");

        for (var i = 0; i < ipt.length; i++)

        {

            ipt.disabled = true;

        }

    }

 

    function setChild(div)

    {

        //可否移動、調整

        var isMove = document.getElementById("isMove").checked;

        var isResize = document.getElementById("isResize").checked;

 

        //底色

        var cDiv = document.createElement;

        var backDiv = cDiv("div");

        backDiv.style.cssText = "left: 0px; top: 0px; width: 100%; height: 100%; background-color: #F5F5F5;";

        div.appendChild(backDiv);

 

        //標題

        var topDiv = cDiv("div");

        topDiv.style.cssText = "left: 2px; top: 2px; width: 100%; height: 30px; position: absolute; background-color: #78ABFF; vertical-align: middle; z-index: 5";

        if (isMove)

        {

            topDiv.style.cursor = "move";

            topDiv.setAttribute("onmousedown", function () { setMove(this) });

        }

        else

        {

            topDiv.style.cursor = "default";

        }

        topDiv.innerHTML = "<span style='top: 5px; left:5px; font-size: 20px; font-weight: bold; color: #102548; position: relative;' onselectstart='return false'>標題欄</span>";

        div.appendChild(topDiv);

 

        //關閉按鈕

        var closeDiv = cDiv("div");

        closeDiv.style.cssText = "right: 8px; top : 5px; width: 24px; height: 24px; position: absolute; background-color: #E4EEFF; border: #2D66C4 1px solid; text-align: center; vertical-align: middle; cursor: pointer; z-index: 10";

        closeDiv.setAttribute("onclick", function () { eCloseDiv() });

        closeDiv.innerHTML = "<span style='font-size: 20px; font-weight: bold; color: #0E377A;' title='Esc快捷鍵'>×</span>";

        div.appendChild(closeDiv);

 

        //內容

        var contentDiv = cDiv("div");

        contentDiv.style.cssText = "left: 2px; top: 35px; width: 100%; position: absolute; overflow: auto";

        contentDiv.style.height = (parseInt(div.style.height) - 40) + "px";

        contentDiv.innerHTML = "<table style='width: 100%; height: 100%; text-align: center; vertical-align: hidden'><tr><td><p>這里是內容區!</p><a href='javascript:saveDiv()'>保留這個位置和大小</a></td></tr></table>";

        div.appendChild(contentDiv);

 

        //調整大小

        var reDiv = cDiv("div");

        reDiv.style.cssText = "right: 0px; bottom: 0px; width: 5px; height: 5px; position: absolute;";

        if (isResize)

        {

            reDiv.style.cursor = "se-resize";

            reDiv.setAttribute("onmousedown", function () { setResize(this) });

        }

        else

        {

            reDiv.style.cursor = "default";

        }

        div.appendChild(reDiv);

 

        return div;

    }

 

    var oX, oY, oLeft, oTop, oWidth, oHeight; //存儲原始移動前的位置

    var divClone, oDiv;   //克隆的節點和原始節點

    var oTime;

    //clone拖移的節點

    function setMove(obj)

    {

        if (event.button == 1)

        {

            if (oTime)

            {

               clearTimeout(oTime);

                divClone.parentNode.removeChild(divClone);

            }

            oDiv = obj.parentNode;

            divClone = oDiv.cloneNode(true);

            divClone.style.filter = "Alpha(opacity=50)";

            divClone.childNodes[1].setAttribute("onmousemove", function () { startMove(this) });

            divClone.childNodes[1].setAttribute("onmouseup", function () { endMove() });

            oX = parseInt(event.clientX);

            oY = parseInt(event.clientY);

            oLeft = parseInt(divClone.style.left);

            oTop = parseInt(divClone.style.top);

            document.body.appendChild(divClone);

            divClone.childNodes[1].setCapture();

            eventType = "move";

        }

    }

 

    //拖移

    function startMove(obj)

    {

        if (eventType == "move" && event.button == 1)

        {

            var moveDiv = obj.parentNode;

            moveDiv.style.left = (oLeft + event.clientX - oX) + "px";

            moveDiv.style.top = (oTop + event.clientY - oY) + "px";

        }

    }

 

    //拖移結束調用動畫

    function endMove()

    {

        if (eventType == "move")

        {

            divClone.childNodes[1].releaseCapture();

            move(parseInt(divClone.style.left), parseInt(divClone.style.top));

            eventType = "";

        }

    }

 

    //移動的動畫

    function move(aimLeft, aimTop)

    {

        var nowLeft = parseInt(oDiv.style.left);

        var nowTop = parseInt(oDiv.style.top);

        var moveSize = 30;

        if (nowLeft > aimLeft + moveSize || nowLeft < aimLeft - moveSize || nowTop > aimTop + moveSize || nowTop < aimTop - moveSize)

        {

            oDiv.style.left = aimLeft > nowLeft + moveSize ? (nowLeft + moveSize) + "px" : aimLeft < nowLeft - moveSize ? (nowLeft - moveSize) + "px" : nowLeft + "px";

            oDiv.style.top = aimTop > nowTop + moveSize ? (nowTop + moveSize) + "px" : aimTop < nowTop - moveSize ? (nowTop - moveSize) + "px" : nowTop + "px";

            oTime = setTimeout("move(" + aimLeft + ", " + aimTop + ")", 1);

        }

        else

        {

            oDiv.style.left = divClone.style.left;

            oDiv.style.top = divClone.style.top;

            divClone.parentNode.removeChild(divClone);

            divClone == null;

        }

    }

 

    //clone調整大小的節點

    function setResize(obj)

    {

        if (event.button == 1)

        {

            if (oTime)

            {

                clearTimeout(oTime);

                divClone.parentNode.removeChild(divClone);

            }

            oDiv = obj.parentNode;

            divClone = oDiv.cloneNode(true);

            divClone.style.filter = "Alpha(opacity=50)";

            divClone.childNodes[4].setAttribute("onmousemove", function () { startResize(this) });

            divClone.childNodes[4].setAttribute("onmouseup", function () { endResize() });

            oX = parseInt(event.clientX);

            oY = parseInt(event.clientY);

            oWidth = parseInt(divClone.style.width);

            oHeight = parseInt(divClone.style.height);

            document.body.appendChild(divClone);

            divClone.childNodes[4].setCapture();

            eventType = "resize";

        }

    }

 

    //拖動調整大小

    function startResize(obj)

    {

        if (eventType == "resize" && event.button == 1)

        {

            var nX = event.clientX;

            var nY = event.clientY;

            if (nX > oX - oWidth && nY > oY - oHeight + 40)

            {

                var resizeDiv = obj.parentNode;

                resizeDiv.style.width = (oWidth + event.clientX - oX) + "px";

                resizeDiv.style.height = (oHeight + event.clientY - oY) + "px";

                resizeDiv.childNodes[3].style.height = (parseInt(resizeDiv.style.height) - 40) + "px";

            }

        }

    }

 

    //調整大小結束

    function endResize()

    {

        if (eventType == "resize")

        {

            divClone.childNodes[4].releaseCapture();

            resize(parseInt(divClone.style.width), parseInt(divClone.style.height));

            eventType = "";

        }

    }

 

    //調整大小的動畫

    function resize(aimWidth, aimHeight)

    {

        var nowWidth = parseInt(oDiv.style.width);

        var nowHeight = parseInt(oDiv.style.height);

        var resizeSize = 30;

        if (nowWidth > aimWidth + resizeSize || nowWidth < aimWidth - resizeSize || nowHeight > aimHeight + resizeSize || nowHeight < aimHeight - resizeSize)

        {

            oDiv.style.width = aimWidth > nowWidth + resizeSize ? (nowWidth + resizeSize) + "px" : aimWidth < nowWidth - resizeSize ? (nowWidth - resizeSize) + "px" : nowWidth + "px";

            oDiv.style.height = aimHeight > nowHeight + resizeSize ? (nowHeight + resizeSize) + "px" : aimHeight < nowHeight - resizeSize ? (nowHeight - resizeSize) + "px" : nowHeight + "px";

            oDiv.childNodes[3].style.height = (parseInt(oDiv.style.height) - 40) + "px";

            oTime = setTimeout("resize(" + aimWidth + ", " + aimHeight + ")", 1);

        }

        else

        {

            oDiv.style.width = divClone.style.width;

            oDiv.style.height = divClone.style.height;

            oDiv.childNodes[3].style.height = (parseInt(oDiv.style.height) - 40) + "px";

            divClone.parentNode.removeChild(divClone);

            divClone == null;

        }

    }

 

    //關閉DIV

    function eCloseDiv()

    {

        if (div)

        {

            div.parentNode.removeChild(div);

            var ipt = document.getElementsByTagName("input");

            for (var i = 0; i < ipt.length; i++)

            {

                ipt.disabled = false;

            }

            div = null;

        }

    }

 

    //保留位置和大小

    function saveDiv()

    {

        if (div)

        {

            saveLeft = div.style.left;

            saveTop = div.style.top;

            saveWidth = div.style.width;

            saveHeight = div.style.height;

            alert("保留成功!");

        }

    }

 

    //快捷鍵

    document.onkeydown = function ()

    {

        event.keyCode == 27 ? eCloseDiv() : null;   //Esc快捷鍵

        event.ctrlKey && (event.keyCode == 83 || event.keyCode == 115) ? saveDiv() : null; //ctrl+s保存位置

        event.ctrlKey && event.keyCode == 13 ? setDiv() : null //ctrl+enter打開Div

        !event.ctrlKey && (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) ? arrowMove(event.keyCode) : null;

        event.ctrlKey && (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) ? arrowResize(event.keyCode) : null;

    }

 

    //上下左右箭頭移動div

    function arrowMove(eKeyCode)

    {

        if (div)

        {

            var isMove = document.getElementById("isMove").checked;

            if (isMove)

            {

                switch (eKeyCode)

                {

                    case 37:

                        div.style.left = (parseInt(div.style.left) - 1) + "px"; //left

                        break

                    case 38:

                        div.style.top = (parseInt(div.style.top) - 1) + "px"; //up

                        break

                    case 39:

                        div.style.left = (parseInt(div.style.left) + 1) + "px"; //right

                        break

                    case 40:

                        div.style.top = (parseInt(div.style.top) + 1) + "px"; //down

                        break

                }

            }

        }

    }

 

    //ctrl+上下左右箭頭調整div大小

    function arrowResize(eKeyCode)

    {

        if (div)

        {

            var isResize = document.getElementById("isResize").checked;

            if (isResize)

            {

                switch (eKeyCode)

                {

                    case 37:

                        div.style.width = (parseInt(div.style.width) - 1) + "px"; //left

                        break

                    case 38:

                        div.style.height = (parseInt(div.style.height) - 1) + "px"; //up

                        break

                    case 39:

                        div.style.width = (parseInt(div.style.width) + 1) + "px"; //right

                        break

                    case 40:

                        div.style.height = (parseInt(div.style.height) + 1) + "px"; //down

                        break

                }

            }

        }

    }

</script>

<body>

<p>

<input type="checkbox" id="isMove" /><label for="isMove">可移動</label>    

<input type="checkbox" id="isResize" /><label for="isResize">可調整大小</label>    

</p>

  

<p>

<input type="radio" name="radio" id="radio1" checked /><label for="radio1">默認居中打開</label>    

<input type="radio" name="radio" id="radio2" /><label for="radio2">保留位置上打開</label>

</p>

<p><a href="javascript:setDiv()">打開DIV</a></p>

<p style="font-weight: bold">操作說明:</p>

<ol>

<li>

   選中復選框(可移動、可調整大小)后,打開的DIV具有移動/調整大小的功能(此時移動/調整大小快捷鍵可使用);<br>

   反之,不可移動/調整大小(此時移動/調整大小快捷鍵無效)

</li>

<li>

   單選框默認居中打開選中,無論有無保存DIV位置和大小均以默認位置及大小打開DIV

</li>

<li>

   單選框保留位置上打開選中,如果未發現保存記錄以默認方式打開,否則以保存的位置及大小打開

</li>

<li>

   如果選中可移動后,鼠標經過標題位置(藍色背景)處會變成移動的圖標,此時按住鼠標左鍵會產生一個半透明的DIV,按住鼠標不動拉動此半透明DIV會跟隨移動,釋放鼠標左鍵后會產生一段原始位置移動至半透明位置的動畫,當動畫重疊時,半透明的DIV將回收。

</li>

<li>

   如果選中可調整大小后,鼠標經過整個DIV的最右下角處會變成伸縮的圖標,此時按住鼠標左鍵會產生一個半透明的DIV,按住鼠標不動拉動此半透明DIV會隨着移動而調整大小,釋放鼠標左鍵后會產生一段原始大小伸縮至半透明大小的動畫,當動畫重疊時,半透明的DIV將回收。

</li>

<li>

   DIV的右上角有關閉按鈕,按下后DIVremove

</li>

</ol>

<p style="font-weight: bold">

快捷鍵說明:

<ol>

<li>

   Ctrl + Enter : 打開div

</li>

<li>

   Ctrl + s(大小寫均可) : 保存div的大小和位置

</li>

<li>

   上下左右箭頭 : 輕移div位置

</li>

<li>

   Ctrl + 上下左右箭頭 : 輕調div大小

</li>

<li>

   Esc : 關閉Div

</li>

</ol>

</p>

</body>

</html>

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM