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