vue2.x实现鼠标拖拽div右下角进行缩放的功能

<template>
  <div ref="box" class="box">
    <div id="handle" ref="handle" draggable="false" @mousedown="handleMousedown" />
  </div>
</template>

<script>
export default {
  name: 'Drag',
  data() {
    return {
      disX: 0,
      disY: 0,
      down: false
    }
  },
  mounted() {
    document.addEventListener('mouseup', () => {
      this.down = false
      document.onmousemove = null
      document.onmousedown = null
      document.onmouseup = null
    })
    document.addEventListener('mousemove', (e) => {
      // 获取鼠标的实时位置
      const curX = e.clientX
      const curY = e.clientY
      // 获取拖拽的长度
      const increaseX = curX - this.disX
      const increaseY = curY - this.disY
      // 按下开始拖拽改变目标元素大小
      if (this.down) {
        const width = this.$refs.handle.offsetWidth + increaseX
        const height = this.$refs.handle.offsetHeight + increaseY
        width < 200 ? (this.$refs.box.style.width = '200px') : (this.$refs.box.style.width = width + 'px')
        height < 200 ? (this.$refs.box.style.height = '200px') : (this.$refs.box.style.height = height + 'px')
      }
    })
  },
  methods: {
    // down置为true,记录鼠标点下的位置
    handleMousedown(e) {
      this.down = true
      this.disX = e.clientX - this.$refs.handle.offsetLeft
      this.disY = e.clientY - this.$refs.handle.offsetTop
    }
  }
}
</script>

<style scoped>
.box {
  width: 200px;
  height: 200px;
  background-color:#000;
  margin: 0 auto;
  margin-top: 100px;
  position: relative;
}

#handle {
  width: 20px;
  height: 20px;
  background-color: red;
  position: absolute;
  right: 0;
  bottom: 0;
  -webkit-user-select: none;
  -moz-user-select: none;
  -o-user-select: none;
  user-select: none;
  cursor: se-resize;
}
</style>

限制最小缩放尺寸是宽高200px

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗倩楠_666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值