文章出处链接
拖动的元素在鼠标按下时通过时间差给元素添加一个属性(drag-flag),若超过规定时间则认为是在拖拽,属性设置为true,否则设置为false。点击方法中通过获取这个属性判断是否要执行点击后的指令。
1 2 3 4
| <div ref="cellBtn" class="cell-btn" :draggable="true" @mousedown.stop="moveBtn" @click.stop="showDialog"> <img src="1.png"/> <span>悬浮按钮</span> </div>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| showDialog(){ const drag = this.$refs.cellBtn.getAttribute('drag-flag') if (drag==='false') { } }, moveBtn(e) { const draggableIcon = this.$refs.cellBtn; if (!draggableIcon) return; draggableIcon.setAttribute('drag-flag', false);
let lastX, lastY if (this.varCallFrom.state!==0) { lastX = e.clientX - draggableIcon.offsetLeft } lastY = e.clientY - draggableIcon.offsetTop
const start = new Date().getTime(); document.onmousemove = () => { const end = new Date().getTime(); if (end - start > 200) { draggableIcon.setAttribute('drag-flag', true); } } const moveFun = (e2) => { e2.preventDefault(); const newX = e2.clientX - lastX const newY = e2.clientY - lastY
draggableIcon.style.left = `${newX}px` draggableIcon.style.top = `${newY}px` }
const stopFun = () => { draggableIcon.removeEventListener('mousemove', moveFun); draggableIcon.removeEventListener('mouseup', stopFun); }
draggableIcon.addEventListener('mousemove', moveFun); draggableIcon.addEventListener('mouseup', stopFun); },
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| .cell-btn{ position: fixed; right: 50px; bottom: 25px; z-index: 101; background: #30314C; box-shadow: 0px 2px 12px 0px rgba(67,61,142,0.5); border-radius: 50%; color: #fff; display: flex; flex-direction: column; justify-content: center; font-size: 14px; height: 80px; width: 80px; text-align: center; cursor: pointer;
img { height: 30px; width: 30px; margin-left: auto; margin-right: auto; margin-bottom: 6px; } }
|