var tz=document.getElementById('tz'); tz.onmousedown=function(e){ var opp=eevent; var xxx=opp.clientX-tz.offsetLeft; var yyy=opp.clientY-tz.offsetTop; // alert(xxx+'...'+yyy); document.onmousemove=function(e){ var opp=eevent; var left=opp.clientX-xxx; var top=opp.clientY-yyy; if(left< 0){ left=0; } if(top< 0){ top=0; } var maxw=document.body.clientWidthdocument.documentElement.clientWidth-tz.offsetWidth;//求最大宽,若有兼容性问题,在body也documentElement之间切换,或者格式 var maxh=document.body.clientHeightdocument.documentElement.clientHeight-tz.offsetHeight;//求最大高,若有兼容性问题,在body也documentElement之间切换,或者格式 if(left>maxw){ left=maxw; } if(top>maxh){ top=maxh; } tz.style.left=left+'px'; tz.style.top=top+'px'; } } tz.onmouseup=function(){ // tz.onmousemove=null;//快速移动,鼠标会脱落 document.onmousemove=null;//快速移动,鼠标不会脱落,上边的onmousemove前边的tz,要换成document }