效果图
在这里插入图片描述
在这里插入图片描述
一个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>
Logo

前往低代码交流专区

更多推荐