当前位置:首页 > VUE

vue实现div拉伸

2026-01-12 00:29:39VUE

Vue 实现 Div 拉伸

使用鼠标事件监听实现拖拽拉伸

在 Vue 中实现 Div 拉伸可以通过监听鼠标事件来动态调整 Div 的尺寸。以下是具体实现方法:

vue实现div拉伸

<template>
  <div 
    class="resizable-div" 
    :style="{ width: width + 'px', height: height + 'px' }"
    @mousedown="startResize"
  >
    <div class="resizer"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      width: 200,
      height: 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.width = e.clientX
        this.height = e.clientY
      }
    },
    stopResize() {
      this.isResizing = false
      document.removeEventListener('mousemove', this.resize)
      document.removeEventListener('mouseup', this.stopResize)
    }
  }
}
</script>

<style>
.resizable-div {
  position: relative;
  border: 1px solid #ccc;
}

.resizer {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 10px;
  height: 10px;
  background: #000;
  cursor: se-resize;
}
</style>

使用第三方库 vue-resizable

对于更复杂的拉伸需求,可以使用 vue-resizable 库:

vue实现div拉伸

npm install vue-resizable

在组件中使用:

<template>
  <resizable
    :width="200"
    :height="200"
    :min-width="100"
    :min-height="100"
    @resize:end="onResizeEnd"
  >
    <div class="content">可拉伸内容</div>
  </resizable>
</template>

<script>
import { Resizable } from 'vue-resizable'

export default {
  components: { Resizable },
  methods: {
    onResizeEnd(size) {
      console.log('新尺寸:', size)
    }
  }
}
</script>

实现四边拉伸功能

如果需要实现四边都能拉伸的效果,可以扩展第一种方法:

<template>
  <div class="resizable-box" :style="boxStyle">
    <div class="resizer top" @mousedown="startResize('top')"></div>
    <div class="resizer right" @mousedown="startResize('right')"></div>
    <div class="resizer bottom" @mousedown="startResize('bottom')"></div>
    <div class="resizer left" @mousedown="startResize('left')"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      width: 300,
      height: 200,
      resizing: null,
      startX: 0,
      startY: 0,
      startWidth: 0,
      startHeight: 0
    }
  },
  computed: {
    boxStyle() {
      return {
        width: `${this.width}px`,
        height: `${this.height}px`
      }
    }
  },
  methods: {
    startResize(direction, e) {
      this.resizing = direction
      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.resizing) return

      const dx = e.clientX - this.startX
      const dy = e.clientY - this.startY

      switch(this.resizing) {
        case 'right':
          this.width = this.startWidth + dx
          break
        case 'bottom':
          this.height = this.startHeight + dy
          break
        case 'left':
          this.width = this.startWidth - dx
          break
        case 'top':
          this.height = this.startHeight - dy
          break
      }
    },
    stopResize() {
      this.resizing = null
      document.removeEventListener('mousemove', this.doResize)
      document.removeEventListener('mouseup', this.stopResize)
    }
  }
}
</script>

<style>
.resizable-box {
  position: relative;
  border: 1px solid #000;
}

.resizer {
  position: absolute;
  background: #000;
}

.resizer.top {
  top: 0;
  left: 0;
  right: 0;
  height: 5px;
  cursor: n-resize;
}

.resizer.right {
  top: 0;
  right: 0;
  bottom: 0;
  width: 5px;
  cursor: e-resize;
}

.resizer.bottom {
  bottom: 0;
  left: 0;
  right: 0;
  height: 5px;
  cursor: s-resize;
}

.resizer.left {
  top: 0;
  left: 0;
  bottom: 0;
  width: 5px;
  cursor: w-resize;
}
</style>

注意事项

  1. 性能优化:频繁的 DOM 操作可能影响性能,可以考虑使用防抖或节流技术
  2. 边界检查:添加最小/最大尺寸限制,防止元素过度缩小或放大
  3. 移动端适配:如果需要支持移动设备,需要添加触摸事件处理
  4. 组件销毁:在组件销毁时移除事件监听器,避免内存泄漏

标签: vuediv
分享给朋友:

相关文章

vue实现多级弹窗

vue实现多级弹窗

Vue 多级弹窗实现方法 方法一:使用动态组件与递归组件 在 Vue 中可以通过动态组件配合递归组件实现多级弹窗。定义一个基础弹窗组件,通过递归调用自身实现层级嵌套。 基础弹窗组件模板示例:…

vue实现自动翻译

vue实现自动翻译

Vue 实现自动翻译的方法 在 Vue 项目中实现自动翻译功能可以通过多种方式完成,以下是几种常见的方法: 使用第三方翻译 API 注册并获取翻译 API 的密钥(如 Google Translat…

vue表格实现教学

vue表格实现教学

Vue 表格实现方法 使用原生HTML表格 在Vue模板中直接使用HTML的<table>标签,结合v-for动态渲染数据: <template> <table&…

vue实现条件判断

vue实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式…

vue弹幕实现不重叠

vue弹幕实现不重叠

实现 Vue 弹幕不重叠的方法 CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。 // 弹幕轨道高度计算 const t…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm ins…