• vue+mousemove实现拖动,鼠标移动过快拖动就失效


    今天用vue+原生js的mousemove事件,写了个拖动,发现只能慢慢拖动才行,鼠标只要移动快了,就失效,不能拖动了;

    搞了半天在,总算解决了,但是问题的深层原理还没搞清楚,知道的大侠可以留言分享,下面直接上代码:

    只能慢速拖动的代码:

    <!DOCTYPE html>
    <html>
    <head>
        <title>vue结合原生js实现拖动</title>
    <script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
    <div class="ctn ctn1">
        <div class="sub sub1" v-for="(site, index) in list1">
             <div class="dragCtn fixed" @mousedown="mousedown(site, $event)" @mousemove.prevent='mousemove(site, $event)' @mouseup='mouseup(site, $event)'>
              {{ site.name }}
             </div>
       </div>
    </div>
    
    <div class="ctn ctn2">
        <div class="sub sub2" v-for="(site, index) in list2">
             <div class="dragCtn">
                 {{ index }} : {{ site.name }}
             </div>
       </div> 
    </div>   
       
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        list1: [{name:'拖动我', index:0}],
        list2: [{name:'a', index:0}, {name:'b', index:1}, {name:'c', index: 2}, {name:'d', index: 3}],
        vm:'',
        sb_bkx: 0,
        sb_bky: 0,
        is_moving: false
      },
      methods: {
          mousedown: function (site, event) {
            var startx=event.x;
            var starty=event.y;
            this.sb_bkx=startx - event.target.offsetLeft;
            this.sb_bky=starty - event.target.offsetTop;
            this.is_moving = true;
          },
          mousemove: function (site, event) {
              var endx=event.x - this.sb_bkx;
            var endy=event.y - this.sb_bky;
            var _this = this
            if(this.is_moving){
                event.target.style.left=endx+'px';
                event.target.style.top=endy+'px';
            }
          },
          mouseup: function (e) {
            this.is_moving = false;
          }
      }
    })
    </script>
    
    <style>
        .ctn{
            line-height: 50px;
            cursor: pointer;
            font-size: 20px;
            text-align: center;
            float: left;
        }
        .sub:hover{
            background: #e6dcdc;
            color: white;
            width: 100px;
        }
           .ctn1{
               border: 1px solid green;
               width: 100px;
           }
           .ctn2{
               border: 1px solid black;
               width: 100px;
               margin-left: 50px;
           }
           .fixed{
             width: 100px;
             height: 100px;
          position: fixed;
          background: red;
          left: 10px;
          top: 10px;
          cursor: move;
           }
    </style>
    </body>
    </html>

    可以快速拖动的代码:

    <!DOCTYPE html>
    <html>
    <head>
        <title>vue结合原生js实现拖动</title>
    <script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
    </head>
    <body>
    <div id="app">
    <div class="ctn ctn1">
    <!-- draggable=true -->
        <div class="sub sub1" v-for="(site, index) in list1">
        <!-- @mousemove.prevent='mousemove(site, $event)' -->
             <div class="dragCtn fixed" @mousedown="mousedown(site, $event)" @mouseup='mouseup(site, $event)'>
                 {{ site.name }}
             </div>
       </div>
    </div>
    
    <div class="ctn ctn2">
        <div class="sub sub2" v-for="(site, index) in list2">
             <div class="dragCtn">
                 {{ index }} : {{ site.name }}
             </div>
       </div> 
    </div>   
       
    </div>
    
    <script>
    new Vue({
      el: '#app',
      data: {
        list1: [{name:'拖动我', index:0}],
        list2: [{name:'a', index:0}, {name:'b', index:1}, {name:'c', index: 2}, {name:'d', index: 3}],
        vm:'',
        sb_bkx: 0,
        sb_bky: 0,
      },
      methods: {
          mousedown: function (site, event) {
            var event=event||window.event;
            var _target = event.target
            var startx=event.clientX;
            var starty=event.clientY;
            var sb_bkx=startx-event.target.offsetLeft;
            var sb_bky=starty-event.target.offsetTop;
            var ww=document.documentElement.clientWidth;
            var wh = window.innerHeight;
    
            if (event.preventDefault) {
                event.preventDefault();
            } else{
                event.returnValue=false;
            };
    
            document.onmousemove=function (ev) {
                var event=ev||window.event;
                var scrolltop=document.documentElement.scrollTop||document.body.scrollTop;
                if (event.clientY < 0 || event.clientX < 0 || event.clientY > wh || event.clientX > ww) {
                    return false;
                };
                var endx=event.clientX-sb_bkx;
                var endy=event.clientY-sb_bky;
                _target.style.left=endx+'px';
                _target.style.top=endy+'px';
            }
          },
    
          mouseup: function (e) {
            document.onmousemove=null;
          }
      }
    })
    </script>
    
    <style>
        .ctn{
            line-height: 50px;
            cursor: pointer;
            font-size: 20px;
            text-align: center;
            float: left;
        }
        .sub:hover{
            background: #e6dcdc;
            color: white;
            width: 100px;
        }
           .ctn1{
               border: 1px solid green;
               width: 100px;
           }
           .ctn2{
               border: 1px solid black;
               width: 100px;
               margin-left: 50px;
           }
           .fixed{
          width: 100px;
             height: 100px;
          position: fixed;
          background: red;
          left: 10px;
          top: 15px;
          cursor: move;
           }
    </style>
    </body>
    </html>

     vue+ 原生js拖动这块还要继续研究,待续...

  • 相关阅读:
    C++——string转char[]
    Ackerman的非递归算法(未解决)
    单链表——递归求最大整数、节点个数、平均值
    队列——以数组Q[m]存放循环队列元素,设置一个标志tag,以tag=0和tag=1来区别在头指针和尾指针相等时,队列为空或满
    队列——假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点(注意:不设头指针), * 试编写相应的置空队列、判断队列是否为空、入队和出队等算法。
    栈——判断回文
    栈——表达式求值
    栈——匹配()[]
    栈——十进制转八进制
    动态获取导航栏
  • 原文地址:https://www.cnblogs.com/yalong/p/9529463.html
Copyright © 2020-2023  润新知