vue拖拽改变宽度

18 篇文章 1 订阅

 1.封装组件ResizeBox.vue

<template>
  <div ref="resize" class="resize">
    <div ref="resizeHandle" class="handle-resize" />
    <slot />
  </div>
</template>
<script>
export default {
  name: 'ResizeBox',
  props: {
    resizeConf: {
      type: Object,
      default: () => ({
        width: 280, // 初始宽度
        widthRange: [100, 500] // 宽度范围
      })
    }
  },
  mounted() {
    this.dragControllerDiv(this.$refs.resize, this.$refs.resizeHandle)
  },
  methods: {
    dragControllerDiv: function(resizeBox, resizeHandle) {
      resizeBox.style.width = this.resizeConf.width + 'px'
      // 鼠标按下事件
      resizeHandle.onmousedown = e => {
        const resizeWidth = resizeBox.offsetWidth
        const startX = e.clientX // 水平坐标
        // 鼠标拖动事件
        document.onmousemove = ev => {
          const moveX = ev.clientX
          const moveLen = resizeWidth + (moveX - startX)
          if (this.resizeConf.widthRange[0] <= moveLen && this.resizeConf.widthRange[1] >= moveLen) {
            resizeBox.style.width = moveLen + 'px'
          }
        }
        // 鼠标松开事件
        document.onmouseup = function() {
          document.onmousemove = null
          document.onmouseup = null
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.resize {
  background: #fbfbfb;
  position: relative;
  word-wrap: break-word;

  .handle-resize {
    cursor: col-resize;
    position: absolute;
    right: -2px;
    width: 6px;
    height: 50px;
    border-left: 2px solid #c5c5c5;
    border-right: 2px solid #c5c5c5;
    top: calc(50% - 25px);
  }
}
</style>

 2.组件中使用

<template>
  <div class="container sa-flex">
    <ResizeBox :resize-conf="resizeConf">
      我是左边我是左边我是左边我是左边我是左边我是左边我是左边我是左边我是左边我是左边
    </ResizeBox>
    <div class="right sa-flex-1">
      我是右边我是右边我是右边我是右边我是右边我是右边我是右边我是右边我是右边我是右边
    </div>
  </div>
</template>
<script>
import ResizeBox from './ResizeBox.vue'
export default {
  components: {
    ResizeBox
  },
  data() {
    return {
      resizeConf: {
        width: 280, // 初始宽度
        widthRange: [100, 500] // 宽度范围
      }
    }
  }
}

</script>
<style lang="scss">
.sa-flex {
  display: flex;
  flex-wrap: no-wrap
}
.sa-flex-1 {
  flex:1
}
.container {
  min-height: 600px;
  background: #eee;
}
</style>

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值