拖拽

作者: 心存美好 | 来源:发表于2022-03-30 10:27 被阅读0次
<!DOCTYPE HTML>
<html>

<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <meta name="keywords" content="关键词">
  <meta name="description" content="描述">
  <meta name="author" content="">
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    body {
      user-select: none;
    }
    .content {
      position: fixed;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      width: 320px;
      height: 207px;
      margin: auto;
    }

    .content ul {
      list-style-type: none;
    }

    .content ul li {
      position: absolute;
    }

    .content ul li img {
      display: block;
      width: 300px;
      border: 10px solid #958a0d;
      cursor: pointer;
    }
  </style>
</head>

<body>
  <div class="content">
    <ul>
      <li><img src="images/1.jpg" alt=""></li>
      <li><img src="images/2.jpg" alt=""></li>
      <li><img src="images/3.jpg" alt=""></li>
      <li><img src="images/4.jpg" alt=""></li>
      <li><img src="images/5.jpg" alt=""></li>
    </ul>
  </div>

  <script>
    // 获取元素
    let oCon = document.querySelector('.content')
    let aLi = [...document.getElementsByTagName('li')];

    //定义一些其他变量
    let zIndex = 0;
    let winW, winH, leftMax, topMax;

    //获取拖拽元素的宽高
    let w = oCon.clientWidth;
    let h = oCon.clientHeight;

    //封装一个计算最大值和最小值的函数
    function ltMax() {
      let wObj = getViewportOffset();
      winW = wObj.w;
      winH = wObj.h;
      leftMax = (winW - w) / 2;
      topMax = (winH - h) / 2;
    }
    ltMax()
    window.onresize = ltMax;//浏览器窗口发生变化就触发window.onresize

    //封装获取浏览器屏幕的宽高
    function getViewportOffset() {
      if (window.innerWidth) {//主流浏览器
        return {
          w: window.innerWidth,
          h: window.innerHeight
        }
      } else {
        if (document.compatMode === 'CSS1Compat') {
          return {//标准盒模型
            w: document.documentElement.clientWidth,
            h: document.documentElement.clientHeight
          }
        } else {//怪异盒模型
          return {
            w: document.body.clientWidth,
            h: document.body.clientHeight
          }
        }
      }
    }
    //批量绑定事件

    aLi.forEach((oLi, index) => {
      oLi.onmousedown = function (ev) {//给每个oLi绑定事件onmousedown事件,并拿到事件对象ev
        ev = ev || window.event//兼容
        ev.preventDefault()//图片默认带拖拽残影行为,清除默认
        //获取鼠标按下时距离浏览器左侧和顶部的距离
        let startX = ev.clientX;
        let startY = ev.clientY;
        //获取被拖拽的li元素距离浏览器左侧,顶部距离
        let startL = this.offsetLeft;
        let startT = this.offsetTop;
        // let This = this;//外侧定义This,在内存使用,也可以采用强制绑定外侧this,不定义This
        //绑定鼠标移动事件,绑定到document上,保证只要鼠标在页面上,事件始终会执行
        document.onmousemove = function (ev) {
          ev = ev || window.event//兼容
          //获取鼠标移动后的距离
          let nowX = ev.clientX;
          let nowY = ev.clientY;
          //计算容器的偏移 
          let nowL = nowX - startX + startL;
          let nowT = nowY - startY + startT;
          //边界检测
          nowL = Math.max(nowL, -leftMax)//水平
          nowL = Math.min(nowL, leftMax)
          nowT = Math.max(nowT, -topMax)//垂直
          nowT = Math.min(nowT, topMax)


          //给元素设置新的位置
          // This.style.top = nowT + 'px';//使用外部This
          // This.style.left = nowL + 'px';
          // This.style.zIndex = ++zIndex;//谁被点击谁的zIndex就加加

          this.style.top = nowT + 'px';//强制绑定外部this方法.bind(this)
          this.style.left = nowL + 'px';
          this.style.zIndex = ++zIndex;

        }.bind(this)
        //解绑事件
        document.onmouseup = function (ev) {
          this.onmousemove = null;
          this.onmouseup = null;
        }

      }
    });


  </script>
</body>

</html>

相关文章

  • 拖拽操作

    应用: 1.拖拽排序2.拖拽上传3.拖拽裁剪 拖拽流程 确定可拖拽的内容-->开始拖拽-->拖拽过程-->结束拖拽...

  • Html5 + Css 3 文件拖拽上传功能

    拖拽上传文件功能 拖拽事件 拖拽元素ondrag 应用于拖拽元素,整个拖拽过程都会调用ondragstart应用...

  • 拖拽上传

    拖拽事件 拖拽元素ondrag 应用于拖拽元素,整个拖拽过程都会调用ondragstart应用于拖拽元素,...

  • js:拖拽事件

    拖拽事件 ondragstart当拖拽元素被开始拖拽时,event:拖拽元素。[从操作系统向浏览器拖拽文件不会触发...

  • HTML 5 拖拽

    @(HTML5)[HTML 5拖拽] [TOC] 十三、HTML 5 拖拽 HTML 5 拖拽事件 图片自带拖拽功...

  • JQuery UI 拖拽排序

    禁用拖拽: 启用拖拽:

  • MacOS 开发(十六) : 文件拖拽

    文件拖拽的核心是拖拽目标视图 (DragDestinationView),此方法会检测目标是否可拖拽类型,拖拽文件...

  • HTML5拖拽上传

    传统拖拽效果小demohtml5实现拖拽小demo调查问卷小demo拖拽拼图小demo拖拽上传小demo h5拖拽...

  • HTML5拖拽drag

    通过拖拽实现页面元素的位置改变 实现拖拽效果 源元素 - 要拖拽的文件 目标元素 - 要拖拽到哪里去 目前实现拖拽...

  • 元素拖曳 H5

    拖拽 如何让一个元素变成拖拽元素 draggable=‘true’ ondrag 应用于拖拽元素,整个拖拽过程都会...

网友评论

      本文标题:拖拽

      本文链接:https://www.haomeiwen.com/subject/jzpijrtx.html