vue中拖动元素边框改变宽度
效果图一个form表单(换成你自己需要拖动的模块),一个自定义的组件Xhandle。然后flex。在Xhandle里写入拖动方法,然后将拖动的值传递给父组件,并将宽度计算后赋值给需要拖动的模块。Xhandle代码如下:<template><div class="x-handle" @mousedown="mouseDown"></div>&l...
·
效果图
一个form表单(换成你自己需要拖动的模块),一个自定义的组件Xhandle。然后flex。
在Xhandle里写入拖动方法,然后将拖动的值传递给父组件,并将宽度计算后赋值给需要拖动的模块。
Xhandle代码如下:
<template>
<div class="x-handle" @mousedown="mouseDown"></div>
</template>
<script>
export default {
name: "XHandle",
data() {
return {
lastX: ""
};
},
created() {
document.addEventListener("mouseup", this.mouseUp);
},
destroyed() {
document.removeEventListener("mouseup", this.mouseUp);
},
methods: {
mouseDown(event) {
document.addEventListener("mousemove", this.mouseMove);
this.lastX = event.screenX;
},
mouseMove(event) {
this.$emit("widthChange", this.lastX - event.screenX);
this.lastX = event.screenX;
console.log(this.lastX, "...", event.screenX);
},
mouseUp() {
this.lastX = "";
document.removeEventListener("mousemove", this.mouseMove);
}
}
};
</script>
<style lang="less">
.x-handle {
width: 2px;
cursor: w-resize;
z-index: 10;
background: #ccc;
}
</style>
父组件引用,并绑定方法
<div class='sss'>
<div :style="{ width: width + 'px' }" ></div> // 这里是你自己的组件
<XHandle class="myxhandle" @widthChange="widthChange" />
</div>
<script>
import XHandle from "@/components/Xhandle";
export default {
data() {
return {
width: 700,
};
},
components: {
XHandle
},
methods: {
widthChange(movement) {
console.log(movement, "~~~");
this.width -= movement;
if (this.width < 300) {
this.width = 300;
}
if (this.width > 700) {
this.width = 700;
}
}
}
};
</script>
<style lang="less" scoped>
.sss {
display: flex;
}
</style>
更多推荐
已为社区贡献5条内容
所有评论(0)