核心代码 :
<! 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: Absolute; Left: 100px; Top: 100px; Cursor: Pointer;} </style> </head> <body> <Div Id = "Box"> </div> <script> var Doc. {ancho: math.max (document.documentelement.clientwidth, document.documentelement.scrollwidth), altura: math.max (document.documentelement.clientheight, document.documentelement.scrollheight)};} comparenum de funciones (x, minnum, maxnum) {switch (verdadero) {case) {case) x <minnum: x = minnum; ruptura; caso x> maxnum: x = maxnum; break; predeterminado: x; } return 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 (). HIED-100; this.startl = parseint (this.style.left); this.startt = parseint (this.style.top); //alert(e.clientx) this.startx = e.clientx; this.Starty = e.clienty; // alerta (maxl+':'+maxt+':'+l+':'+t) doc.onmouseMove = function (e) {posxy (e, 0,0, maxl, maxt); }; doc.onmouseUp = function () {doc.onmouseMove = null; doc.onmouseup = null; }; return false;} función 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 需刷新才能执行]