vue----拖拽小方块

话不多说,直接来实例
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');//手动挂载!! };

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容