<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.min.js"></script>
</head>
<style>
#app {
position: relative;
}
.box {
position: absolute;
background: red;
width: 200px;
height: 200px;
}
.green {
background: greenyellow;
}
</style>
<body>
<div id="app">
<div class="box" v-drag></div>
<div class="box green" v-drag></div>
</div>
</body>
<script>
let app = new Vue({
el: '#app',
data: {},
methods: {},
directives: {
drag: {
bind: function(el) {
el.onmousedown = (e) => {
let disX = e.clientX - el.offsetLeft;
let disY = e.clientY - el.offsetTop;
document.onmousemove = (e) => {
let left = e.clientX - disX;
let top = e.clientY - disY;
this.positionX = top;
this.positionY = left;
el.style.left = left + 'px';
el.style.top = top + 'px';
};
document.onmouseup = (e) => {
document.onmousemove = null;
document.onmouseup = null;
};
};
},
},
},
});
</script>
</html>
基于Vue实现拖拽效果之拖拽指令
最新推荐文章于 2024-08-09 11:26:56 发布