vue拖拽组件自定义指令,解决拖拽和点击事件冲突问题,解决拖拽组件在iframe上面延迟卡顿问题

文章介绍了如何在Vue中创建一个自定义指令`v-drag`来实现拖拽功能,同时处理了拖拽元素在iframe上的卡顿问题,通过设置`pointerEvents`属性。当鼠标抬起时,会检测是否触发了点击事件,并调用`showScreenInteraction`方法。
摘要由CSDN通过智能技术生成

在这里插入图片描述

1.自定义指令

directives: {
     drag(el,data,vnode) {
      const oDiv = el
      oDiv.onmousedown = e => {
      	// 获取ifream,解决拖拽组件在ifream上面卡顿-根据ifream pointerEvents 
      	let iframDiv = document.getElementById("screenProjection")
        if(iframDiv) {
            console.log('按下')
            iframDiv.style.pointerEvents = "none"
        }
        const disY = e.pageY - oDiv.offsetTop
        document.onmousemove = e => {
        //解决onmouseup事件有时候不触发
        if ( el.stopPropagation ) {
            el.stopPropagation()
        }
        if ( el.preventDefault ) {
            el.preventDefault()
        }
          let curT = e.pageY - disY
          const scrModelEle = document.getElementsByClassName('screen-model')[0]
          let minT = 0,maxT = scrModelEle.clientHeight - el.offsetHeight
          //超出盒子问题
          curT = curT<minT?minT:(curT > maxT ? maxT : curT)
          oDiv.style.top = curT + 'px'
        }
        document.onmouseup = e2 => {
          if(iframDiv) {
            iframDiv.style.pointerEvents = "auto"
          }
          document.onmousemove = null
          document.onmouseup = null
          if ( e.clientX == e2.clientX && e.clientY == e2.clientY ) {
          		//点击拖拽冲突
			  vnode.context.showScreenInteraction()
		  }
        }
        return false
      }
    }

2.结构-拖拽元素加上v-drag;不绑定点击事件

<div  v-drag ></div>

3.点击事件写在methods

showScreenInteraction() {
  
},
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值