首頁 > web前端 > js教程 > JavaScript實作可拖曳的拖曳層Div實例_javascript技巧

JavaScript實作可拖曳的拖曳層Div實例_javascript技巧

WBOY
發布: 2016-05-16 15:47:03
原創
1096 人瀏覽過

本文實例講述了JavaScript實作可拖曳的拖曳層Div。分享給大家供大家參考。具體如下:

這是一個完美的JS拖曳效果,帶拖曳的JavaScript拖曳層程式碼,經過了多次優化修正,複製節點的方法不錯,值得JS愛好者學習,同時程式碼修正了給拖曳元素加ondblclick事件無效的問題,相容於多種瀏覽器,拷貝程式碼即可運作使用。

運作效果如下圖:

具體程式碼如下:

<!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>无标题文档</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" &#63; $(obj) : obj;
    var _style = node.currentStyle &#63; 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 &#63; (_new - _old) : (_old - _new);
        var speed = length / time * _tt;
        var flag = 0;
        var anim = setInterval(function() {
         node.style[n] = count + "px";
         count = _old <= _new &#63; 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程式設計有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板