话不多说,直接来实例
1.html代码如下:
<div id="box"> <div v-drag class="box1"></div> <div v-drag class="box2"></div> </div>
2.css样式如下:
div.box1{width:100px; height:100px; background:red; position:absolute; top:0;left:0; cursor:move;} div.box2{width:100px; height:100px; background:blue; position:absolute; top:0;right:0; cursor:move;}
3.vue代码如下:
Vue.directive('drag',function(){ var oDiv=this.el; oDiv.onmousedown=function(ev){ var disX=ev.clientX-oDiv.offsetLeft; var disY=ev.clientY-oDiv.offsetTop; document.onmousemove=function(ev){ var l=ev.clientX-disX; var t=ev.clientY-disY; oDiv.style.left=l+'px'; oDiv.style.top=t+'px'; }; document.onmouseup=function(){ document.onmousemove=document.onmouseup=null; }; } }) window.onload=function(){ var vm=new Vue({ data:{ }, methods:{ } }).$mount('#box');//手动挂载!! };
网友评论