JavaScript拖動(dòng)層Div代碼
效果圖:(灰色區(qū)域可拖動(dòng))
代碼如下:
<!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>無(wú)標(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>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
使用BootStrap建立響應(yīng)式網(wǎng)頁(yè)——通欄輪播圖(carousel)
這篇文章主要介紹了使用BootStrap建立響應(yīng)式網(wǎng)頁(yè)通欄輪播圖(carousel)的相關(guān)資料,需要的朋友可以參考下2016-12-12js+html5實(shí)現(xiàn)canvas繪制橢圓形圖案的方法
這篇文章主要介紹了js+html5實(shí)現(xiàn)canvas繪制橢圓形圖案的方法,涉及html5圖形繪制的基礎(chǔ)技巧,感興趣的朋友可以參考一下2016-05-05Easy.Ajax 部分源代碼 支持文件上傳功能, 兼容所有主流瀏覽器
下面是Easy.Ajax類的初稿,如須發(fā)表,在代碼上還要修改以達(dá)到最簡(jiǎn),但API是不會(huì)變了2011-02-02詳解Javascript?基于長(zhǎng)連接的服務(wù)框架問(wèn)題
本文針對(duì)經(jīng)常使用長(zhǎng)連接進(jìn)行消息收發(fā)的應(yīng)答場(chǎng)景,采用 Websocket 長(zhǎng)連接作為服務(wù)監(jiān)聽的對(duì)象,模擬了一套類 http 服務(wù)框架,通過(guò)實(shí)例代碼介紹了Javascript?基于長(zhǎng)連接的服務(wù)框架,需要的朋友可以參考下2022-07-07bootstrap日期控件問(wèn)題(雙日期、清空等問(wèn)題解決)
這篇文章主要介紹了bootstrap日期控件(雙日期、清空等問(wèn)題解決)相關(guān)知識(shí),需要的朋友可以參考下2017-04-04layui radio點(diǎn)擊事件實(shí)現(xiàn)input顯示和隱藏的例子
今天小編就為大家分享一篇layui radio點(diǎn)擊事件實(shí)現(xiàn)input顯示和隱藏的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09