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拖动这块还要继续研究,待续...
vue+mousemove实现拖动,鼠标移动过快拖动就失效的更多相关文章
- jQuery与vue分别实现超级简单的绿色拖动验证码功能
jquery的绿色拖动验证功能 在网上看到了一个这样的问题:那种像拖动滑块匹配图形的验证方式是怎么实现的?. 突然想到实现一个简单绿色拖动验证码的功能,在网上搜了下,有一个用jquery实现的该功能代 ...
- js 鼠标左键拖动滚动
鼠标左键拖动滚动 原作者: http://blog.csdn.net/lisatisfy/article/details/6606026 本文在源代码的基础上 增加支持水平滚动 的功能 html &l ...
- C# GDI绘制矩形框,鼠标左键拖动可移动矩形框,滚轮放大缩小矩形框
最近工作需要,要做一个矩形框,并且 用鼠标左键拖动矩形框移动其位置.网上查了一些感觉他们做的挺复杂的.我自己研究一天,做了一个比较简单的,发表出来供大家参考一下.如觉得简单,可路过,谢谢.哈哈. 先大 ...
- ChromiumWebBrowser禁止鼠标右键和拖动
在屏蔽之前先查看namespace CefSharp.WinForms内的代码 public class ChromiumWebBrowser : Control, IWebBrowserIntern ...
- 从零开始,开发一个 Web Office 套件(9):拖动鼠标选中文字 Edge Case
这是一个系列博客,最终目的是要做一个基于 HTML Canvas 的.类似于微软 Office 的 Web Office 套件(包括:文档.表格.幻灯片--等等). 博客园:<从零开始, 开发一 ...
- vue drag 对表格的列进行拖动排序
用drag实现拖动表格列进行列排序 以下是用到的主要方法 1.dragstart 拖动开始返回目标对象 2.dragenter 拖动过程中经过的对象 3.dragend 拖动结束返回目标对象 ...
- ECharts外部调用保存为图片操作及工作流接线mouseenter和mouseleave由于鼠标移动速度过快导致问题解决办法
记录两个项目开发中遇到的问题,一个是ECharts外部调用保存为图片操作,一个是workflow工作流连接曲线onmouseenter和onmouseleave事件由于鼠标移动过快触发问题. 一.外部 ...
- WM_SYSCOMMAND包括很多功能,比如:拖动左边框、拖动标题栏、滚动条滚动、点击最小化、双击标题栏——Delphi 通过事件代替了大部分常用的消息,所以Delphi 简单、易用、高效
procedure TForm1.WMSysCommand(var Message: TWMSysCommand); var str: string; begin case Message.CmdTy ...
- 原生js中获取this与鼠标对象以及vue中默认的鼠标对象参数
1.通过原生js获取this对象 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
随机推荐
- sql server mvp 發糞塗牆
http://blog.csdn.net/dba_huangzj/article/details/38295753
- 无法通过windows installer服务安装此安装程序包。您必须安装带有更新版本windows Installer服务的Windows
无法通过windows installer服务安装此安装程序包.您必须安装带有更新版本windows installer服务的Windows 出现这个问题不让安装程序,可以到微软网站更新Windows ...
- json格式在线解析
地址:https://www.bejson.com/ { "PN": "123456", "DomainName": ".gxyc ...
- python之生成excel
#_*_coding:utf-8_*_ import MySQLdb import xlwt from datetime import datetime def get_data(sql): # 创建 ...
- Http协议三次握手过程
Http协议三次握手过程 TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: ...
- 配置Linux实现静态路由
配置Linux实现静态路由 背景和原理 路由器的功能是实现一个网段到另一个网段之间的通信,路由分为静态路由.动态路由. 默认路由和直连路由.静态路由是手工指定的,使用静态路由的好处是网络安全保密性高. ...
- python中ascii码和字符的转换
将ASCII字符转换为对应的数值即‘a’-->65,使用ord函数,ord('a') 反正,使用chr函数,将数值转换为对应的ASCII字符,chr(65)
- S5:桥接模式 Bridge
将抽象的部分与实现的部分分离,使它们都可以独立变化.抽象与实现的分离,指的是抽象类和派生类用来实现自己的对象.实现系统可能有多角度分类,每一种分类都有可能变化,那么就把这种多角度分离出来,让他们独立变 ...
- C2:抽象工厂 Abstract Factory
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 应用场景: 一系列相互依赖的对象有不同的具体实现.提供一种“封装机制”来避免客户程序和这种“多系列具体对象创建工作”的紧耦合 UM ...
- Laravel之备忘项(不定期更新)
1.自定义字段验证错误信息 $this->validate($request, ['name' => 'required|max:50'], ['name.required' => ...