当前位置:首页 > VUE

vue实现div拉伸

2026-02-09 13:15:02VUE

实现 div 拉伸的基本思路

在 Vue 中实现 div 拉伸功能,通常需要结合鼠标事件(如 mousedownmousemovemouseup)和动态样式绑定。核心是通过监听鼠标移动来调整 div 的宽度或高度。

水平拉伸 div 的代码示例

以下是一个实现 div 水平拉伸的 Vue 组件示例:

<template>
  <div class="container">
    <div class="left-panel" :style="{ width: leftWidth + 'px' }">左侧内容</div>
    <div class="resize-handle" @mousedown="startResize"></div>
    <div class="right-panel">右侧内容</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      leftWidth: 200,
      isResizing: false
    }
  },
  methods: {
    startResize(e) {
      this.isResizing = true
      document.addEventListener('mousemove', this.resize)
      document.addEventListener('mouseup', this.stopResize)
    },
    resize(e) {
      if (this.isResizing) {
        this.leftWidth = e.clientX
      }
    },
    stopResize() {
      this.isResizing = false
      document.removeEventListener('mousemove', this.resize)
      document.removeEventListener('mouseup', this.stopResize)
    }
  }
}
</script>

<style>
.container {
  display: flex;
  height: 100vh;
}
.left-panel {
  background: #f0f0f0;
}
.resize-handle {
  width: 10px;
  background: #ccc;
  cursor: col-resize;
}
.right-panel {
  flex: 1;
  background: #e0e0e0;
}
</style>

垂直拉伸 div 的实现

要实现垂直拉伸,只需调整相关逻辑和样式:

<template>
  <div class="vertical-container">
    <div class="top-panel" :style="{ height: topHeight + 'px' }">顶部内容</div>
    <div class="resize-handle-vertical" @mousedown="startResize"></div>
    <div class="bottom-panel">底部内容</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      topHeight: 200,
      isResizing: false
    }
  },
  methods: {
    startResize(e) {
      this.isResizing = true
      document.addEventListener('mousemove', this.resize)
      document.addEventListener('mouseup', this.stopResize)
    },
    resize(e) {
      if (this.isResizing) {
        this.topHeight = e.clientY
      }
    },
    stopResize() {
      this.isResizing = false
      document.removeEventListener('mousemove', this.resize)
      document.removeEventListener('mouseup', this.stopResize)
    }
  }
}
</script>

<style>
.vertical-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.top-panel {
  background: #f0f0f0;
}
.resize-handle-vertical {
  height: 10px;
  background: #ccc;
  cursor: row-resize;
}
.bottom-panel {
  flex: 1;
  background: #e0e0e0;
}
</style>

双向拉伸实现

要实现同时支持水平和垂直拉伸的 div:

<template>
  <div class="resizable-box" 
       :style="{ width: width + 'px', height: height + 'px' }">
    <div class="content">可拉伸内容</div>
    <div class="resize-handle-right" @mousedown="startResize('right')"></div>
    <div class="resize-handle-bottom" @mousedown="startResize('bottom')"></div>
    <div class="resize-handle-corner" @mousedown="startResize('both')"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      width: 300,
      height: 200,
      isResizing: false,
      resizeDirection: null,
      startX: 0,
      startY: 0,
      startWidth: 0,
      startHeight: 0
    }
  },
  methods: {
    startResize(direction, e) {
      this.resizeDirection = direction
      this.isResizing = true
      this.startX = e.clientX
      this.startY = e.clientY
      this.startWidth = this.width
      this.startHeight = this.height

      document.addEventListener('mousemove', this.doResize)
      document.addEventListener('mouseup', this.stopResize)
    },
    doResize(e) {
      if (this.isResizing) {
        if (this.resizeDirection === 'right' || this.resizeDirection === 'both') {
          this.width = this.startWidth + (e.clientX - this.startX)
        }
        if (this.resizeDirection === 'bottom' || this.resizeDirection === 'both') {
          this.height = this.startHeight + (e.clientY - this.startY)
        }
      }
    },
    stopResize() {
      this.isResizing = false
      document.removeEventListener('mousemove', this.doResize)
      document.removeEventListener('mouseup', this.stopResize)
    }
  }
}
</script>

<style>
.resizable-box {
  position: relative;
  border: 1px solid #ccc;
  background: #f8f8f8;
}
.content {
  padding: 20px;
}
.resize-handle-right {
  position: absolute;
  right: 0;
  top: 0;
  width: 5px;
  height: 100%;
  cursor: col-resize;
}
.resize-handle-bottom {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 5px;
  cursor: row-resize;
}
.resize-handle-corner {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 10px;
  height: 10px;
  cursor: nwse-resize;
}
</style>

性能优化建议

在实现拉伸功能时,频繁的 DOM 操作可能影响性能。可以考虑以下优化措施:

vue实现div拉伸

  • 使用 requestAnimationFrame 替代直接的样式更新
  • 添加防抖或节流处理
  • 避免在 mousemove 事件中进行复杂的计算
  • 在组件销毁时移除事件监听器
// 在组件中添加
beforeDestroy() {
  document.removeEventListener('mousemove', this.doResize)
  document.removeEventListener('mouseup', this.stopResize)
}

注意事项

  • 确保在组件销毁时移除事件监听器,避免内存泄漏
  • 考虑添加最小和最大尺寸限制
  • 移动端实现可能需要处理触摸事件
  • 复杂的布局可能需要更精细的控制逻辑

标签: vuediv
分享给朋友:

相关文章

vue实现走势图

vue实现走势图

Vue 实现走势图的方法 使用 ECharts 库 ECharts 是一个强大的数据可视化库,支持多种图表类型,包括走势图(折线图)。在 Vue 项目中可以通过 vue-echarts 封装库或直接使…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-ful…

vue实现长按

vue实现长按

Vue 实现长按功能的方法 在 Vue 中实现长按功能可以通过原生事件监听或自定义指令完成。以下是几种常见实现方式: 使用原生事件监听 通过 @mousedown 和 @mouseup 或 @tou…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…