vue项目中,实现拖拽动态改变元素宽高

需求效果

在这里插入图片描述

代码实现

html&css

<template>
  <div id="app">
    <!-- 左 -->
    <div class="left">
      <div class="left-top" />
      <!-- 左上 -->
      <div class="resizeBar h" ref="resizeTopBar" />
      <!-- 左下 -->
      <div class="left-bottom" />
    </div>
    <div class="resizeBar v" ref="resizeLeftBar" />

    <!-- 中 -->
    <div class="medium" />

    <!-- 右 -->
    <div class="resizeBar v" ref="resizeRightBar" />
    <div class="right" />
  </div>
</template>
<style>
body,
html,
#app {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

#app {
  display: flex;
}

.left {
  flex: 0 0 200px;
  display: flex;
  flex-direction: column;
}

.left-top {
  flex: 0 0 50%;
  background-color: lightsteelblue;
}

.left-bottom {
  flex: 1;
  background-color: burlywood;
}

.medium {
  flex: 1
}

.right {
  flex: 0 0 200px;
  background-color: bisque;
}

.resizeBar {
  background-color: #000;
}

.resizeBar.v {
  width: 5px;
  height: 100%;
}

.resizeBar.h {
  width: 100%;
  height: 5px;
}

.resizeBar.v:hover {
  cursor: col-resize
}

.resizeBar.h:hover {
  cursor: row-resize
}
</style>

js

<script>

export default {
  name: 'App',
  mounted() {
    this.resizeTopAndBottom()
    this.resizeLeftAndMedium()
    this.resizeRightAndMedium()
  },
  methods: {
    // 左上-左下之间的拖动,改变左上的高度,左下设置flex:1;高度自适应
    resizeTopAndBottom() {
      const resizeElement = document.querySelector('.left-top')
      this.$refs['resizeTopBar'].onmousedown = e => {
        // left-top初始高度
        const oldHeight = resizeElement.offsetHeight
        // 点击鼠标初始Y值
        const startY = e.clientY
        document.onmousemove = e => {
          // 当前鼠标Y值
          const endY = e.clientY
          // 当前鼠标在Y轴移动的距离
          const distance = endY - startY
          // left-top当前高度
          const newHeight = oldHeight + distance
          // 设置dom样式
          resizeElement.style.flex = `0 0 ${newHeight}px`
        }
        document.onmouseup = () => {
          document.onmousemove = null;
          document.onmouseup = null;
        }
        return false
      }
    },
     // 左-中之间的拖动,改变左的宽度,中设置flex:1;宽度自适应
     resizeLeftAndMedium() {
      const resizeElement = document.querySelector('.left')
      this.$refs['resizeLeftBar'].onmousedown = e => {
        // left初始宽度
        const oldWidth = resizeElement.offsetWidth
        // 点击鼠标初始X值
        const startX = e.clientX
        document.onmousemove = e => {
          // 当前鼠标X值
          const endX = e.clientX
          // 当前鼠标在X轴移动的距离
          const distance = endX - startX
          // left-top当前高度
          const newWidth = oldWidth + distance
          // 设置dom样式
          resizeElement.style.flex = `0 0 ${newWidth}px`
        }
        document.onmouseup = () => {
          document.onmousemove = null;
          document.onmouseup = null;
        }
        return false
      }
    },
     // 中-右之间的拖动,改变右的宽度,中设置flex:1;宽度自适应
     resizeRightAndMedium() {
      const resizeElement = document.querySelector('.right')
      this.$refs['resizeRightBar'].onmousedown = e => {
        // left初始宽度
        const oldWidth = resizeElement.offsetWidth
        // 点击鼠标初始X值
        const startX = e.clientX
        document.onmousemove = e => {
          // 当前鼠标X值
          const endX = e.clientX
          // 当前鼠标在X轴移动的距离
          const distance = startX - endX
          // left-top当前高度
          const newWidth = oldWidth + distance
          // 设置dom样式
          resizeElement.style.flex = `0 0 ${newWidth}px`
        }
        document.onmouseup = () => {
          document.onmousemove = null;
          document.onmouseup = null;
        }
        return false
      }
    }
  }
}
</script>

思路

以 左-中-右 三列布局为例

  1. 样式上,采用flex布局比较便捷,左右给一个初始的固定宽,中就flex:1;,这样左右宽度改变中也能自适应改变宽度
  2. 在左-中,中-右,之间添加一个元素作为拖动的抓手,通过拖动抓手来改变布局元素的宽高
  3. 拖动抓手
    • 抓手onmousedown事件,获取鼠标点击时的初始X值或Y值,以及要改变宽高元素的初始宽高
    • document onmousemove事件,通过鼠标移动,实时获取当前鼠标X或Y值,计算鼠标实时的水平/垂直移动距离,计算元素的实时宽高,并设置样式
    • document onmouseup,鼠标抬起,拖动结束,清空事件
  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue可以通过绑定mousedown、mousemove、mouseup事件来实现拖拽边框改变元素宽度。具体步骤如下: 1. 绑定mousedown事件,在事件处理函数记录鼠标点击的初始位置和被拖拽元素的初始宽度。 2. 绑定mousemove事件,在事件处理函数计算鼠标的相对移动距离,并根据鼠标移动距离更新被拖拽元素宽度。 3. 绑定mouseup事件,在事件处理函数取消mousemove和mouseup事件的绑定。 下面是示例代码: ``` <template> <div class="drag-wrapper"> <div class="drag-target" :style="{ width: targetWidth + 'px' }"> Drag Me </div> <div class="drag-handle" @mousedown="handleDown"></div> </div> </template> <script> export default { data () { return { isDragging: false, startX: 0, startWidth: 0, targetWidth: 200 } }, methods: { handleDown (e) { this.isDragging = true this.startX = e.clientX this.startWidth = this.targetWidth document.addEventListener('mousemove', this.handleMove) document.addEventListener('mouseup', this.handleUp) }, handleMove (e) { if (!this.isDragging) return const deltaX = e.clientX - this.startX this.targetWidth = this.startWidth + deltaX }, handleUp () { this.isDragging = false document.removeEventListener('mousemove', this.handleMove) document.removeEventListener('mouseup', this.handleUp) } } } </script> <style scoped> .drag-wrapper { position: relative; height: 100px; padding: 10px; background-color: #ddd; } .drag-target { height: 100%; background-color: #fff; text-align: center; line-height: 80px; font-size: 20px; border: 1px solid #333; } .drag-handle { position: absolute; top: 0; right: -5px; bottom: 0; width: 10px; background-color: #333; cursor: ew-resize; } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值