vue3自定义指令实现拖拽

src/directives下index.js的文件配置

在这里插入图片描述

// 自定义指令
export default {
    install(app) {
        app.directive('drag', {
            mounted(el, bind, vnode) { // 绑定的元素,绑定的值
                // 这个事件主要是用于禁止选择网页中的文字
                document.onselectstart = function () {
                    return false
                }
                el.onmousedown = function (e) {
                    // 鼠标按下,计算当前元素距离可视区的距离
                    let disX = e.clientX
                    let disY = e.clientY
                    let offTop = e.target.offsetTop
                    let offLeft = e.target.offsetLeft
                    document.onmousemove = function (e) {
                        // 通过事件委托,计算移动的距离
                        let l = e.clientX - disX + offLeft
                        let t = e.clientY - disY + offTop
                        const width = document.querySelector('.list').clientWidth
                        const top = document.querySelector('.list').clientHeight
                        console.log(l,"llll");
                        console.log(t,"ttt");
                        if (l >= width - 70) {
                            l = width - 70
                        } else if (l <= 0) {
                            l = 0
                        }
                        if (t >= top - 70) {
                            t = top - 70
                        } else if (t <= 0) {
                            t = 0
                        }
                        el.style.left = l
                        el.style.top = t
                        bind.value.top = t
                        bind.value.left = l
                    }
                    document.onmouseup = function () {
                        document.onmousemove = null
                        document.onmouseup = null
                    }
                    return false
                }
            }
        })
    }
}

main.js文件中的配置

在这里插入图片描述

在.vue文件中的使用

<template>
  <dl class="list">
    <dd
      v-for="(item, key) in totallist.list"
      :key="key"
      v-drag="item"
      :style="{ left: item.left + 'px', top: item.top + 'px' }"
    >
      {{ item.title }}
      <img
        :src="item.img"
        alt=""
      />
    </dd>
  </dl>
</template>

<script setup>
import { reactive } from "vue";
const totallist = reactive({
  list: [
    {
      title: "张三",
      img: "https://cdn.staticaly.com/gh/1024huijia/QingChunMeizi@master/10010.4i2i1hvz85k0.webp",
      top: 50,
      left: 80,
    },
     {
      title: "李四",
      img: "https://cdn.staticaly.com/gh/1024huijia/QingChunMeizi@master/10010.4i2i1hvz85k0.webp",
      top: 80,
      left: 50,
    }
  ],
});
</script>

<style lang="less" scoped>
.list {
  margin: 0 auto;
  margin-top: 5px;
  width: 340px;
  height: 390px;
  border: 1px solid red;
  position: relative;
}
.list dd {
  height: 70px;
  width: 70px;
  background-color: rgb(221, 221, 221);
  margin: 0;
  position: absolute;
}
.list dd img {
  width: 50px;
  height: 50px;
}
</style>

效果

vue自定义指令实现拖拽效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值