当前位置:首页 > VUE

vue实现div拉伸

2026-01-12 00:29:39VUE

Vue 实现 Div 拉伸

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

在 Vue 中实现 Div 拉伸可以通过监听鼠标事件来动态调整 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 库:

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. 组件销毁:在组件销毁时移除事件监听器,避免内存泄漏

vue实现div拉伸

标签: vuediv
分享给朋友:

相关文章

vue实现皮肤切换

vue实现皮肤切换

实现皮肤切换的基本思路 在Vue中实现皮肤切换功能,通常涉及动态修改CSS变量或类名。核心是通过状态管理当前主题,并在组件中应用对应的样式。 使用CSS变量实现主题切换 CSS变量(自定义属性)是…

vue点击实现排序

vue点击实现排序

实现点击排序的方法 在Vue中实现点击排序功能,可以通过以下几种方式完成: 使用计算属性结合排序方法 定义一个响应式数据存储排序状态,通过计算属性动态返回排序后的数组。点击事件切换排序状态。…

vue实现打字机

vue实现打字机

Vue实现打字机效果 在Vue中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <di…

vue实现访问量

vue实现访问量

Vue 实现访问量统计的方法 使用前端计数 在 Vue 中可以通过简单的计数器实现页面访问量的统计。这种方法适合小型项目或演示用途。 // 在 Vue 组件中 data() { return {…

vue实现横向导航

vue实现横向导航

vue实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…

vue设计与实现目录

vue设计与实现目录

以下是《Vue设计与实现》一书的典型目录结构(根据常见技术书籍及Vue核心内容整理): 响应式系统 响应式数据的基本原理 依赖收集与触发更新 实现简易响应式系统 Proxy与Reflec…