1. 程式人生 > >拖拽 縮放

拖拽 縮放

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #da{
            width:300px;
            height:300px;
            position:relative;
            border:1px solid red;
        }
        #tuo{
            cursor:nw-resize;
            position:absolute;
            right:0px;
            bottom:0px;
            width:15px;
            height:15px;
            z-index:3;
        }
    </style>
</head>
<body>
<div id="da" style="left: 385px; top: 49px; width: 745px; height: 323px;">
  <div id="tuo"></div>
</div>
<script>
    var tuo=document.getElementById("tuo");
    var da=document.getElementById("da");
    var mouseStart={};
    var divStart={};
    //往左右同時拽      右下角拖動
    tuo.onmousedown=function(ev)
    {
        var oEvent=ev||event;          //瀏覽器相容
        mouseStart.x=oEvent.clientX;   //拖動前的x,y值    當前的位置
        mouseStart.y=oEvent.clientY;
        divStart.x=tuo.offsetLeft;     //左側和頂部的距離
        divStart.y=tuo.offsetTop;
        if(tuo.setCapture)            //判斷是不是滑鼠事件
        {
            tuo.onmousemove=doDrag;   //滑鼠事件是    滑鼠按下時
            tuo.onmouseup=stopDrag;   //滑鼠事件是    滑鼠擡起
            tuo.setCapture();
        }
        else
        {
            document.addEventListener("mousemove",doDrag,true);
            document.addEventListener("mouseup",stopDrag,true);
        }
    };
    function doDrag(ev)
    {
        var oEvent=ev||event;
        var l=oEvent.clientX-mouseStart.x+divStart.x;       //當前的寬
        var t=oEvent.clientY-mouseStart.y+divStart.y;       //當前的高
        var w=l+tuo.offsetWidth;                            //offsetWidth拖拉圖表的寬
        var h=t+tuo.offsetHeight;

        if( w < tuo.offsetWidth)
        {
            w=tuo.offsetWidth;
        }
        else if(w>document.documentElement.clientWidth-da.offsetLeft)
        {
            w=document.documentElement.clientWidth-da.offsetLeft-2;
        }
        if(h<tuo.offsetHeight)
        {
            h=tuo.offsetHeight;
        }
        else if(h>document.documentElement.clientHeight-da.offsetTop)
        {
            h=document.documentElement.clientHeight-da.offsetTop-2;
        }
        da.style.width=w+"px";
        da.style.height=h+"px";
    };
    function stopDrag()
    {
        if(tuo.releaseCapture)
        {
            tuo.onmousemove=null;
            tuo.onmouseup=null;
            tuo.releaseCapture();
        }
        else
        {
            document.removeEventListener("mousemove",doDrag,true);
            document.removeEventListener("mouseup",stopDrag,true);
        }
    };


    //拖拽
    da.ondblclick=function(ev)
    {
        var oEvent=ev||event;
        mouseStart.x=oEvent.clientX;
        mouseStart.y=oEvent.clientY;
        divStart.x=da.offsetLeft;
        divStart.y=da.offsetTop;
        if(da.setCapture)
        {
            da.onmousemove=doDrag3;
            da.onmouseup=stopDrag3;
            da.setCapture();
        }
        else
        {
            document.addEventListener("mousemove",doDrag3,true);
            document.addEventListener("mouseup",stopDrag3,true);
        }
    };
    function doDrag3(ev)
    {
        var oEvent=ev||event;
        var l=oEvent.clientX-mouseStart.x+divStart.x;
        var t=oEvent.clientY-mouseStart.y+divStart.y;
        if( l < 0)
        {
            l=0;
        }
        else if(l>document.documentElement.clientWidth-da.offsetWidth)
        {
            l=document.documentElement.clientWidth-da.offsetWidth;
        }
        if(t < 0)
        {
            t=0;
        }
        else if(t>document.documentElement.clientHeight-da.offsetHeight)
        {
            t=document.documentElement.clientHeight-da.offsetHeight;
        }
        da.style.left=l+"px";
        da.style.top=t+"px";
    };
    function stopDrag3()
    {
        if(da.releaseCapture)
        {
            da.onmousemove=null;
            da.onmouseup=null;
            da.releaseCapture();
        }
        else
        {
            document.removeEventListener("mousemove",doDrag3,true);
            document.removeEventListener("mouseup",stopDrag3,true);
        }
    }
</script>
</body>
</html>