实现效果

相关使用属性
实现完整代码
<template>
<div class="to-do-list" ref="parentBox">
<div class="search-title">
<h1 class="add-bold left-box" style="margin-left:35px">
<a-icon type="unordered-list" style="margin-right: 10px;" />元素拖动
</h1>
</div>
<a-button ref="moveBtn" style="width: 100px;height: 40px;transition:none" class="move-btn" type="primary"
>拖动按钮</a-button
>
</div>
</template>
<script lang="ts">
import { Component, Emit, Inject, Prop, Ref, Vue, Watch } from 'vue-property-decorator';
@Component({
components: {},
})
export default class BriberyInformation extends Vue {
@Ref() readonly moveBtn;
@Ref() readonly parentBox;
btnDown() {
let box = this.moveBtn.$el;
let parentBox = this.parentBox;
let parentH = parentBox.clientHeight;
let parentW = parentBox.clientWidth;
let x, y;
let moveX, moveY;
let maxX, maxY;
let isDrop = false;
box.onmousedown = e => {
x = e.clientX - box.offsetLeft;
y = e.clientY - box.offsetTop;
isDrop = true;
};
document.onmousemove = e => {
if (isDrop) {
e.preventDefault();
moveX = e.clientX - x;
moveY = e.clientY - y;
maxX = parentW - box.offsetWidth;
maxY = parentH - box.offsetHeight;
moveX = Math.min(maxX, Math.max(0, moveX));
moveY = Math.min(maxY, Math.max(0, moveY));
box.style.left = moveX + 'px';
box.style.top = moveY + 'px';
} else {
return;
}
};
document.onmouseup = e => {
e.preventDefault();
isDrop = false;
};
}
mounted() {
this.btnDown();
}
}
</script>
<style scoped lang="less">
.to-do-list {
position: relative;
min-height: 600px;
max-height: 600px;
width: 600px;
overflow: hidden;
border: 2px solid black;
.move-btn {
position: absolute;
}
}
</style>
参考来源:
参考来源:用JavaScript实现div的鼠标拖拽效果