版权声明:非经本人同意,请勿转载。 https://blog.csdn.net/QQ_Empire/article/details/81989100
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="../js/vue.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
p{
position: absolute;
top: 10px;
left: 10px;
width: 200px;
height: 200px;
background: red;
overflow: hidden;
}
</style>
</head>
<body>
<div id="out">
<p v-move></p>
<p v-move></p>
<p v-move></p>
</div>
</body>
<script type="text/javascript">
Vue.directive('move',function(a){
a.onmousedown=function(e){
var x=e.clientX-a.offsetLeft
var y=e.clientY-a.offsetTop
document.onmousemove=function(e){
var l=e.clientX-x;
var t=e.clientY-y;
a.style.left=l+'px';
a.style.top=t+'px';
}
document.onmouseup=function(){
document.onmousemove=null;
document.onmousedown=null;
}
}
})
var vm=new Vue({
el:'#out'
})
</script>
</html>