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; } ); }