核心代码:
<!doctype html> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8" " /> <title> </title> <style> #box {width:100px; height:100px; background:#966; position:aspolute; left:100px; top:100px; cursor:pointer;} </head> <body> <body> <div id = "box"> div> </docurcimt; {width:math.max(document.documentelement.clientwidth、document.documentelement.scrollwidth)、height:math.max(document.documentelement.clientheight、document.documentelement.scrollheight.scrollheight)};} function comparenum(x、minnum、mannum) x <minnum:x = minnum; break;ケースx> maxnum:x = maxnum; break;デフォルト:x; } xを返します。 } box.style.left = '100px'box.style.top =' 100px'box.onmousedown = function(e){var e = e || window.event; var maxl = getViewport()。width-100、maxt = getViewport()。height-100; this.startl = parseint(this.style.left); this.startt = parseint(this.style.top); //alert(e.clientx)this.startx = e.clientx; this.starty = e.clienty; // alert(maxl+':'+maxt+':'+l+':'+t)doc.onmousemove = function(e){posxy(e、0,0、maxl、maxt); }; doc.onmouseup = function(){doc.onmousemove = null; doc.onmouseup = null; }; false;} function posxy(e、minl、mint、maxl、maxt){var e = e || window.event; var x = box.startl-(box.startx-e.clientx); var y = box.startt-(box.starty-e.clienty); box.style.left = comparenum(x、minl、maxl)+'px' box.style.top = comparenum(y、mint、maxt)+'px'} </script> </body> </html>[ctrl+a全选全选:如需引入外部js需刷新才能执行]