vue中使用拖拽功能并且解决点击事件和按下事件冲突

在这里插入图片描述
项目需求:使这两个按键可以上下拖动,而且这两个按钮还有点击事件

<span class="action-btn" ref="showDoc" :class="showDoc ? 'hide': 'show'" @click="handShowDoc" @mousedown="mouseDownHandDoc($event)" @mouseup="mouseUpHandDoc($event)">{{ showDoc ? '隐藏' : '显示' }}文档</span>
<span class="action-btn" ref="showTools" :class="showLabTools?'tools-hide': 'tools-show'" @click="handShowTools" @mousedown="mouseDownHandTools($event)" @mouseup="mouseUpHandTools($event)">{{ showLabTools ? '隐藏' : '显示' }}工具栏</span>
export default {
	data () {
		moveDoc: { //显示文档初始位置
                x: null,
                y: null
            },
         moveTools:{ //显示工具栏位置
                x: null,
                y: null
         }
	},
	methods:{
		handShowDoc(){ //判断显示文档是点击事件还是拖拽事件
            let isClick = this.$refs.showDoc.getAttribute('flag')
            if(isClick ==='true') this.showDoc=!this.showDoc
            else return false
        },
        mouseDownHandDoc(event){//显示文档的鼠标按下事件
            this.moveDoc.y = event.pageY - this.$refs.showDoc.offsetTop
            event.currentTarget.style.cursor = 'move'
            window.onmousemove = this.mouseMoveHandDoc
            this.$refs.showDoc.setAttribute('flag', false)
            const firstTime = new Date().getTime()
            document.onmouseup = () => {
                document.onmousemove = null
                document.onmouseup = null
                // onmouseup 时的时间,并计算差值
                const lastTime = new Date().getTime()
                if ((lastTime - firstTime) < 200) {
                    this.$refs.showDoc.setAttribute('flag', true)
                }
            }
        },
        mouseMoveHandDoc (event) { //显示文档的鼠标移动事件
            let moveTop = event.pageY - this.moveDoc.y + 'px'
            this.$refs.showDoc.style.top = moveTop
        },
        mouseUpHandDoc(event){ //显示文档的鼠标抬起事件
            window.onmousemove = null
            event.currentTarget.style.cursor = 'move'
        },
	}
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题可能是由于鼠标事件和 iframe 渲染事件冲突导致的。您可以尝试在拖拽时将 iframe 隐藏或者将其 z-index 设置为 -1,以避免冲突。另外,您可以使用 CSS3 的 transform 属性来实现拖拽,这样可以减少因为重排和重绘导致的卡顿。具体做法如下: 1. 在拖拽开始时,将 iframe 的 z-index 设置为 -1 或者将其隐藏起来: ``` iframe { z-index: -1; /* 或者 */ display: none; } ``` 2. 使用 CSS3 的 transform 属性来实现拖拽: ``` <div class="draggable" @mousedown="handleMouseDown"> <!-- your content here --> </div> <script> export default { data() { return { dragging: false, startX: 0, startY: 0, translateX: 0, translateY: 0, }; }, methods: { handleMouseDown(event) { this.dragging = true; this.startX = event.clientX; this.startY = event.clientY; }, handleMouseMove(event) { if (this.dragging) { this.translateX = event.clientX - this.startX; this.translateY = event.clientY - this.startY; this.$refs.draggable.style.transform = `translate(${this.translateX}px, ${this.translateY}px)`; } }, handleMouseUp(event) { this.dragging = false; }, }, mounted() { document.addEventListener('mousemove', this.handleMouseMove); document.addEventListener('mouseup', this.handleMouseUp); }, beforeDestroy() { document.removeEventListener('mousemove', this.handleMouseMove); document.removeEventListener('mouseup', this.handleMouseUp); }, }; </script> <style scoped> .draggable { position: absolute; top: 0; left: 0; cursor: move; } </style> ``` 注意,这里的 translate 属性是相对于元素自身的位置进行位移,而不是相对于父元素的位置进行位移。如果您需要相对于父元素进行位移,可以使用 position 和 top/left 属性来实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值