原生js、vue超强拖动伸缩插件vue-draggable-resizable实现拖动div窗口功能实现

<div :style="dialogHeight" class="pathfind dialog" ref="dialogRef"
       v-show="visible.isShow" @mousedown="mouseDownHandleelse($event)"  @mouseup="mouseUpHandleelse($event)">

// 内容

</div>
<script>
  export default {
    methods: {
      mouseDownHandleelse(event) {
        // 判断文本框是否聚焦 文本框聚焦时不能移动窗口
        var inputs = document.getElementsByClassName("search-input")
        var flag = false
        for (let i = 0; i < inputs.length; i++) {
          let temp = inputs[i].getElementsByClassName("el-input__inner")
          for (let j = 0; j < temp.length; j++) {
            if (temp[j] == document.activeElement) flag = true
          }
        }
        if (!flag) {
            // 计算当前鼠标在窗口中的位置
          this.moveDataelse.x = event.pageX - this.$refs.dialogRef.offsetLeft
          this.moveDataelse.y = event.pageY - this.$refs.dialogRef.offsetTop
          event.currentTarget.style.cursor = 'move'
          window.onmousemove = this.mouseMoveHandleelse
        }
      },
      mouseMoveHandleelse(event) {
        // 计算窗口拖动后距离上边有右边的距离(我的需求时靠右,可以计算靠左更简单点)
        let moveRight = window.innerWidth - this.$refs.dialogRef.offsetWidth - (event.pageX - this.moveDataelse.x) + 'px'
        let moveTop = event.pageY - this.moveDataelse.y + 'px'
        this.$refs.dialogRef.style.right = moveRight
        this.$refs.dialogRef.style.top = moveTop
      },
      mouseUpHandleelse(event) {
        window.onmousemove = null
        event.currentTarget.style.cursor = 'default'
      }
    }
  }
</script>

vue的 vue-draggable-resizable插件实现

各种强大灵活参数和事件参考官网修改:https://github.com/gorkys/vue-draggable-resizable

<template>
  <div style="height: 500px; width: 500px; border: 1px solid red; position: relative;">
    <vdr :w="100" :h="100" v-on:dragging="onDrag" v-on:resizing="onResize" :parent="true" :min-width="200" :min-height="200">
      <p>Hello! I'm a flexible component. You can drag me around and you can resize me.<br>
        X: {{ x }} / Y: {{ y }} - Width: {{ width }} / Height: {{ height }}</p>
    </vdr>
    <vdr
      :w="200"
      :h="200"
      :parent="true"
      :debug="false"
      :min-width="200"
      :min-height="200"
      :isConflictCheck="true"
      :snap="true"
      :snapTolerance="20"
    >
    </vdr>
  </div>
</template>

<script>
  import vdr from 'vue-draggable-resizable-gorkys'
  import 'vue-draggable-resizable-gorkys/dist/VueDraggableResizable.css'

  export default {
    components: { vdr },
    data: function () {
      return {
        width: 0,
        height: 0,
        x: 0,
        y: 0
      }
    },
    methods: {
      onResize: function (x, y, width, height) {
        this.x = x
        this.y = y
        this.width = width
        this.height = height
      },
      onDrag: function (x, y) {
        this.x = x
        this.y = y
      }
    }
  }
</script>

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值