vue 元素拖拽

效果演示:
请添加图片描述
前置知识理解图示:
在这里插入图片描述
代码1:

<template>
  <div class='container'>
    <div id="box1"></div>
  </div>
</template>

<script>
export default {
  data () {
    return {

    }
  },
  mounted () {
    this.moveEle()
  },
  methods: {
    moveEle () {
      const box1 = document.getElementById('box1')
      box1.onmousedown = (e) => {
        // 计算鼠标到元素X,Y距离
        const X = e.clientX - box1.offsetLeft
        const Y = e.clientY - box1.offsetTop
        const maxWidth = document.body.clientWidth - box1.offsetWidth
        const maxHeight = document.documentElement.clientHeight - box1.offsetHeight
        document.onmousemove = (event) => {
          event = event || window.event
          // 设置偏移量
          let left = event.clientX - X
          let top = event.clientY - Y
          if (left < 0) {
            left = 0
          } else if (left > maxWidth) {
            left = maxWidth
          }
          if (top < 0) {
            top = 0
          } else if (top > maxHeight) {
            top = maxHeight
          }
          // 修改box1的位置
          box1.style.left = left + 'px'
          box1.style.top = top + 'px'
        }
        document.onmouseup = () => {
          // 取消document的onmousemove和onmouseup事件 !!!
          document.onmousemove = null
          document.onmouseup = null
        }
      }
    }

  }
}
</script>

<style scoped lang='scss'>
    #box1{
        width: 100px;
        height: 100px;
        left:10px;
        top:10px;
        background-color: red;
        position: absolute;
    }
</style>

代码2:

<template>
  <div class='container'>
    <div id="box1" @mousedown="moveEle"></div>
  </div>
</template>

<script>
export default {
  data () {
    return {

    }
  },
  methods: {
    moveEle (e) {
      const box1 = document.getElementById('box1')
      // 计算鼠标到元素X,Y距离
      const X = e.clientX - box1.offsetLeft
      const Y = e.clientY - box1.offsetTop
      const maxWidth = document.body.clientWidth - box1.offsetWidth
      const maxHeight = document.documentElement.clientHeight - box1.offsetHeight
      document.onmousemove = (event) => {
        event = event || window.event
        // 设置偏移量
        let left = event.clientX - X
        let top = event.clientY - Y
        // 控制在视图之内
        if (left < 0) {
          left = 0
        } else if (left > maxWidth) {
          left = maxWidth
        }
        if (top < 0) {
          top = 0
        } else if (top > maxHeight) {
          top = maxHeight
        }
        // 修改box1的位置
        box1.style.left = left + 'px'
        box1.style.top = top + 'px'
      }
      document.onmouseup = () => {
        // 取消document的onmousemove和onmouseup事件 !!!
        document.onmousemove = null
        document.onmouseup = null
      }
    }

  }
}
</script>

<style scoped lang='scss'>
    #box1{
        width: 100px;
        height: 100px;
        left:10px;
        top:10px;
        background-color: red;
        position: absolute;
    }
</style>

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用 Vue.js 和一些第三方库来实现拖拽缩放组件,以下是一个基本的实现步骤: 1. 安装第三方库,例如 vue-draggable-resizable 或 vue-draggable 。 2. 在 Vue 组件中引入库并注册组件。 3. 在模板中使用组件,并设置组件的初始位置和大小。 4. 通过组件的事件监听器来跟踪组件的位置和大小变化,并在 Vue 实例中更新组件的状态。 5. 根据组件的状态来动态更新组件的样式和位置。 以下是一个简单的示例代码: ```html <template> <div> <vue-draggable-resizable :x="x" :y="y" :w="w" :h="h" :min-width="100" :min-height="100" :active.sync="isActive" :draggable="true" :resizable="true" @dragging="onDragging" @resizing="onResizing" > <div class="content">可拖拽缩放的元素</div> </vue-draggable-resizable> </div> </template> <script> import VueDraggableResizable from 'vue-draggable-resizable' export default { components: { VueDraggableResizable }, data() { return { x: 0, y: 0, w: 200, h: 200, isActive: true } }, methods: { onDragging(x, y) { this.x = x this.y = y }, onResizing(w, h) { this.w = w this.h = h } } } </script> <style> .content { width: 100%; height: 100%; background-color: #f0f0f0; border: 1px solid #ccc; border-radius: 4px; display: flex; align-items: center; justify-content: center; } </style> ``` 在这个示例中,我们使用了 vue-draggable-resizable 库来创建可拖拽缩放的元素。我们设置了元素的初始位置和大小,并监听了 dragging 和 resizing 事件来更新元素的状态。我们还根据元素的状态来动态更新了元素的样式和位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值