您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“JavaScript拖動層Div的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“JavaScript拖動層Div的示例分析”這篇文章吧。
效果圖:(灰色區(qū)域可拖動)
代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標(biāo)題文檔</title> <style type="text/css"> *{ margin:0; padding:0;} .div{ width:100px; height:100px; position:absolute;left:100px; top:100px; background:#ccc;} </style> </head> <body> <div class="div"> </div> <script type="text/javascript"> var div = document.getElementsByTagName('div')[0]; var zIndex = 6; drag(div); div.ondblclick = function() { alert("ok"); }; function drag(oDrag) { var disX = dixY = 0; oDrag.onmousedown = function(event) { var event = event || window.event; disX = event.clientX - this.offsetLeft; disY = event.clientY - this.offsetTop; var oTemp = this.cloneNode(true); document.body.appendChild(oTemp); document.onmousemove = function(event) { var event = event || window.event; var iL = event.clientX - disX; var iT = event.clientY - disY; var maxL = document.documentElement.clientWidth - oDrag.offsetWidth; var maxT = document.documentElement.clientHeight - oDrag.offsetHeight; iL <= 0 && (iL = 0); iT <= 0 && (iT = 0); iL >= maxL && (iL = maxL); iT >= maxT && (iT = maxT); oTemp.style.zIndex = zIndex++; oTemp.style.opacity = "0.5"; oTemp.style.filter = "alpha(opacity=50)"; oTemp.style.left = iL + "px"; oTemp.style.top = iT + "px"; return false; }; document.onmouseup = function() { document.onmousemove = null; document.onmouseup = null; oDrag.style.opacity = oTemp.style.opacity; var arr = { left: oTemp.offsetLeft, top: oTemp.offsetTop }; oDrag.style.zIndex = oTemp.style.zIndex; oAnimate(oDrag, arr, 300, function() { document.body.removeChild(oTemp); }); oDrag.releaseCapture && oDrag.releaseCapture() }; this.setCapture && this.setCapture(); return false } } function oAnimate(obj, params, time, handler) { var node = typeof obj == "string" ? $(obj) : obj; var _style = node.currentStyle ? node.currentStyle: window.getComputedStyle(node, null); var handleFlag = true; for (var p in params) { (function() { var n = p; if (n == "left" || n == "top") { var _old = parseInt(_style[n]); var _new = parseInt(params[n]); var _length = 0, _tt = 10; if (!isNaN(_old)) { var count = _old; var length = _old <= _new ? (_new - _old) : (_old - _new); var speed = length / time * _tt; var flag = 0; var anim = setInterval(function() { node.style[n] = count + "px"; count = _old <= _new ? count + speed: count - speed; flag += _tt; if (flag >= time) { node.style[n] = _new + "px"; clearInterval(anim); if (handleFlag) { handler(); handleFlag = false; } } }, _tt); } } })(); } } </script> </body> </html>
以上是“JavaScript拖動層Div的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。