div可隨意拖動


   function DragDlg(){
      var helperdialogwrapper =$(".helper-dialog-wrapper");
        var x = 0;
        var y = 0;
        var l = 0;
        var t = 0;
        var isDown = false;
        //鼠標按下事件
        $(".helper-dialog-wrapper").bind("mousedown",function(e) {
            //獲取x坐標和y坐標
            x = e.clientX;
            y = e.clientY;

            //獲取左部和頂部的偏移量
            l = helperdialogwrapper.offset().left;
            t = helperdialogwrapper.offset().top;
            //開關打開
            isDown = true;
            //設置樣式  
        });
        //鼠標移動
        window.onmousemove = function(e) {
            if (isDown == false) {
                return;
            }
            //獲取x和y
            var nx = e.clientX;
            var ny = e.clientY;
            //計算移動后的左偏移量和頂部的偏移量
            var nl = parseInt(l)+(parseInt(nx) -parseInt(x));
            var nt = parseInt(t)+(parseInt(ny) -parseInt(y));
            sss=parseInt(nx) -parseInt(x);
            lll=parseInt(ny) -parseInt(y);
//這里設置offset而不是css,因為獲取時是根據offset獲取的偏移量
            $(".helper-dialog-wrapper").offset({top:nt,left:nl});
        }
        //鼠標抬起事件
        $(".helper-dialog-wrapper").bind("mouseup",function() {
            //開關關閉
            isDown = false;
        }
        );
    }

 


免責聲明!

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



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