Vue+js 給按鈕增加拖動(dòng)手勢(shì)事件

給自定義圖片、按鈕增加手勢(shì)拖動(dòng),并增加彈性效果

html

<div class="directory-mode" ref="matrix_box" type="primary" @touchstart.stop="touchStart" @touchend="touchend" @touchmove.stop="touchMove" @click="showPopover=true" :style="{opacity: alphaValue}">目錄
</div>

js

data() {
    return {
            leftX: 0,
            topY: 0,
            startX: '',
            startY: '',
            alphaValue: '0.5',
            maxX: 0,
            maxH: 0
    }
},
methods: {
        touchStart(event) {
            this.leftX = event.targetTouches[0].pageX - this.startX;
            this.maxX = document.body.clientWidth
            this.maxH = document.body.clientHeight
            this.alphaValue = '1'
            document.body.style.overflow = 'hidden'
            this.startX = event.targetTouches[0].pageX - this.$refs.matrix_box.offsetLeft;
            this.startY = event.targetTouches[0].pageY - this.$refs.matrix_box.offsetTop;
        },
        touchMove(event) {
            this.alphaValue = '1'
            event.stopPropagation()
            this.leftX = event.targetTouches[0].pageX - this.startX;
            this.topY = event.targetTouches[0].pageY - this.startY;
            this.$refs.matrix_box.style.position = 'absolute'
            this.$refs.matrix_box.style.left = this.leftX + "px";
            this.$refs.matrix_box.style.top = this.topY + "px";
        },
        touchend(event) {
            // 根據(jù)自己設(shè)備寬高計(jì)算拖動(dòng)范圍
            if(this.leftX < 0 || this.leftX < (this.maxX - 40)/2) {
                this.leftX = 0
            }else if(this.leftX > (this.maxX - 40)/2) {
                this.leftX = this.maxX - 40
            }
            if(this.topY < -290) {
                this.topY = -290
            }else if(this.topY > (this.maxH - 75 - 310)) {
                this.topY = this.maxH - 75 - 310
            }

            this.$refs.matrix_box.style.position = 'absolute'
            this.$refs.matrix_box.style.left = this.leftX + "px";
            this.$refs.matrix_box.style.top = this.topY + "px";
            document.body.style.overflow = ''
            this.$refs.matrix_box.removeEventListener("touchstart", this.touchStart)
            document.removeEventListener("touchmove", this.touchMove )
            setTimeout(() => {
                this.alphaValue = '0.5'
            }, 1000);
        },
}
       
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容