走自己的路,让别人说去吗!美好的日子带来快乐,阴暗的日子带来经验,所以别对任何一天怀有遗憾。
本文实例为大家分享了Vue实现开关按钮拖拽效果的具体代码,供大家参考,具体内容如下
css:
<style> .box { position: absolute; left: 100px; top: 100px; width: 100px; height: 100px; background: red; } .box2 { position: absolute; left: 400px; top: 100px; width: 100px; height: 100px; background: green; } </style>
html:
<div id="app"> <div class="box" v-drag="isDrag"></div> <div class="box2" v-drag:fn.limit.b="isDrag"></div> <button @click="isDrag = !isDrag">{{isDrag}}</button> </div>
js:
<script src="vue.js"></script> <script> // 组件 Vue.directive('drag', { bind(el, {value, arg, modifiers}) { console.log(value, arg, modifiers) el._value = value; el._isDrag = false; el.addEventListener('mousedown', function(e) { if (!el._value) return; el._isDrag = true; el._x = e.clientX - el.offsetLeft; el._y = e.clientY - el.offsetTop; e.preventDefault(); }); document.addEventListener('mousemove', function(e) { if (el._isDrag) { let l = e.clientX - el._x; let t = e.clientY - el._y; if (modifiers.limit) { if (l < 0) { l = 0; } if (t < 0) { t = 0; } } el.style.left = l + 'px'; el.style.top = t + 'px'; } }); document.addEventListener('mouseup', function(e) { el._isDrag = false; }); }, componentUpdated(el, {value}) { console.log('更新了', value); el._value = value; } }); let app = new Vue({ el: '#app', data: { isDrag: false } }); </script>
到此这篇关于Vue如何实现开关按钮拖拽效果就介绍到这了。一个人身边没有人,就容易寂寞。但一个人心里没有人,才会空虚。更多相关Vue如何实现开关按钮拖拽效果内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!