欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript簡(jiǎn)單拖拽效果(1)

 更新時(shí)間:2017年05月17日 14:25:08   作者:GY_U_YG  
這篇文章主要為大家詳細(xì)介紹了JavaScript簡(jiǎn)單拖拽效果 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

拖拽在前端開(kāi)發(fā)中是很常見(jiàn)的功能,也是基本功之一,本文是不限制范圍的拖拽也就是最簡(jiǎn)單的拖拽,鼠標(biāo)按下對(duì)象,拖拽,松開(kāi)停止!
1,onmousedown事件
2,onmousemove事件
3,onmouseup事件

因?yàn)樵诎聪聲r(shí)拖動(dòng),所以onmousemove事件在down后才注冊(cè),up事件是用來(lái)解綁事件,消除事件!

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>簡(jiǎn)單拖拽</title>
  <style type="text/css">
    * {
      margin: 0;
      padding: 0;
    }
    #div1 {
      width: 100px;
      height: 100px;
      background: orange;
      position: absolute;
    }
  </style>
</head>
<body style="height: 500000px;">
  <div id="div1"></div>

  <script type="text/javascript">
    function getStyle(obj, attr) {
      if (obj.currentStyle) {
        return obj.currentStyle[attr];
      } else {
        return getComputedStyle(obj, null)[attr];
      }
    }
    var oDiv = document.getElementById('div1');
    oDiv.onmousedown = function(ev) {
      var oEvent = ev || event;
      // var disX = oEvent.clientX - oDiv.offsetLeft;
      // var disY = oEvent.clientY - oDiv.offsetTop;

      var disX = oEvent.clientX - parseInt(getStyle(oDiv, 'left'));
      var disY = oEvent.clientY - parseInt(getStyle(oDiv, 'top'));

      document.onmousemove = function(ev) {
        var oEvent = ev || event;
        oDiv.style.left = oEvent.clientX - disX + 'px';
        oDiv.style.top = oEvent.clientY - disY + 'px';
      };
      document.onmouseup = function() {
        document.onmousemove = null;
        document.onmouseup = null;
      };
      return false;
    };
  </script>
</body>
</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論