当前位置:首页 > 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中实现点击旋转轮盘效果,可以通过CSS动画和Vue的数据绑定结合完成。以下是一个完整的实现方案: 准备工作 需要安装Vue.js环境,可以通过CDN引入或使用Vue C…

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-s…

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue优秀实现

vue优秀实现

Vue.js 优秀实现方法 响应式数据管理 使用 Vue 的 ref 和 reactive 处理基础类型和对象类型数据,结合 computed 计算属性优化渲染性能。对于复杂状态,推荐 Pinia 替…

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要…

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-…