码迷,mamicode.com
首页 > 其他好文 > 详细

Vue中实现拖拽

时间:2019-07-26 02:03:34      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:ice   event   utf-8   获取   ons   doc   head   and   htm   

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{margin:0;padding: 0}
#box{
width:100px;
height: 100px;
background: red;
position:absolute;
}
</style>
</head>
<body>
<div id="app">
<div id="box" v-drag.l.t="flag"></div>
</div>//v-drag 中有l t值 则 modifiers中就有这个值
</body>
</html>
<script src="vue.js"></script>
<script>
Vue.directive("drag",(el,{modifiers,value})=>{
console.log(modifiers)
let {l,t} = modifiers;//通过结构赋值 获取l:true t:true
el.addEventListener("mousedown",handleDownCb)

let disX ,disY;
function handleDownCb(e){
disX = e.offsetX;
disY = e.offsetY;
document.addEventListener("mousemove",handleMoveCb);
document.addEventListener("mouseup",handleupCb)
}

function handleMoveCb(e){
let x = e.clientX - disX;
let y = e.clientY - disY;
if((l&&t) && value){
el.style.left = x + ‘px‘;
el.style.top = y + ‘px‘;
return;
}

if(l&&value){
el.style.left = x + ‘px‘;
return;
}

if(t&&value){
el.style.top = y + ‘px‘;
return;
}
}

function handleupCb(){
document.removeEventListener("mousemove",handleMoveCb)
document.removeEventListener("mouseup",handleupCb)
}
})

var vm = new Vue({
el:"#app",
data:{
flag:true
}
})
</script>

 

Vue中实现拖拽

标签:ice   event   utf-8   获取   ons   doc   head   and   htm   

原文地址:https://www.cnblogs.com/superclound/p/11247661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!