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拖动这块还要继续研究,待续...

原文地址:https://www.cnblogs.com/yalong/p/9529463.html

时间: 2024-07-31 18:00:08

vue+mousemove实现拖动,鼠标移动过快拖动就失效的相关文章

js 鼠标左键拖动滚动

鼠标左键拖动滚动 原作者: http://blog.csdn.net/lisatisfy/article/details/6606026 本文在源代码的基础上 增加支持水平滚动 的功能 html <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html

C# GDI绘制矩形框,鼠标左键拖动可移动矩形框,滚轮放大缩小矩形框

最近工作需要,要做一个矩形框,并且 用鼠标左键拖动矩形框移动其位置.网上查了一些感觉他们做的挺复杂的.我自己研究一天,做了一个比较简单的,发表出来供大家参考一下.如觉得简单,可路过,谢谢.哈哈. 先大概介绍一下原因,GDI画矩形框就不说了,很简单的.这里面最主要的就是滚轮放大和左键移动两个事件,要计算矩形框的坐标位置.下面将代码贴出如下: 先是定义需要的变量,就四个变量. //矩形框坐标        private Rectangle DrawRect = new Rectangle(0, 0

拖动鼠标改变div层的大小宽度

<html> <head> <title>拖动鼠标改变div层的大小宽度-石家庄色彩顾问-亿诚</title> <meta content="text/html; charset=gb2312" http-equiv="Content-Type"> <style> { box-sizing: border-box; moz-box-sizing: border-box } #testDiv {

ChromiumWebBrowser禁止鼠标右键和拖动

C#引用CefSharp在C#的设计界面,添加panel控件用来放置CEFSharp浏览器.CEFSharp浏览器dll添加引用 using CefSharp;using CefSharp.WinForms;12CEFSharp浏览器初始化代码: public Form1(){InitializeComponent();InitBrowser();}public ChromiumWebBrowser browser;public void InitBrowser(){Cef.Initialize

原生js中获取this与鼠标对象以及vue中默认的鼠标对象参数

1.通过原生js获取this对象 ``` <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <form action="" class="files" > <label class="file"

WPF 窗体中的 Canvas 限定范围拖动 鼠标滚轴改变大小

xaml代码: 1 <Canvas Name="movBg"> 2 <Canvas.Background> 3 <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 4 <GradientStop Color="White" Offset="0" /> 5 <GradientStop Colo

鼠标拖动虚影效果(拖动的时候使用图片蒙人,但效果不错)

疯狂delphi delphiXE7.XE8.XE10公开课A 群号:58592705 鼠标拖动虚影效果 1 //1.定义消息 2 procedure MYHideMessage(var Msg: tagMSG; var Handled: Boolean); 3 4 //2.执行消息 5 procedure TForm2.MYHideMessage(var Msg: tagMSG; var Handled: Boolean); 6 var 7 pt:TPoint; 8 bit: TBitmap;

AS2.0鼠标跟随和拖动代码

1,鼠标跟随. a: Mouse.hide();//隐藏鼠标,Mouse.show()显示鼠标. MC1.startDrag(true);//直接利用函数实现. b: Mouse.hide(); onEnterFrame=function(){//坐标赋值实现 MC1._x=_xmouse;//鼠标坐标赋值给MC1. MC1._y=_ymouse; } 2,垂直拖动. //垂直拖动在Y(249,260)输出YES. bar.onPress=function() { startDrag(bar,t

javascript中区分鼠标单击和拖动事件

在javascript中,一般的DOM元素如div,都有onmousedown.onmousemove.onmouseup这3个鼠标事件. <div id="div1" onmousedown="down();" onmouseup="up();" onmousemove="move();"></div> 当鼠标在div1上移动或者按下左键拖动的时候,都会触发onmousemove事件.如何区分这2种事