美文网首页
Vue+js 给按钮增加拖动手势事件

Vue+js 给按钮增加拖动手势事件

作者: small_Sun | 来源:发表于2023-04-23 15:58 被阅读0次

给自定义图片、按钮增加手势拖动,并增加弹性效果

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) {
            // 根据自己设备宽高计算拖动范围
            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);
        },
}
       

相关文章

  • iOS事件响应者链、

    一、知识点简介、 1.0 iOS中的事件可以分为三大类型: 1、触屏事件(例如点击按钮、通过手势缩放图片、拖动上下...

  • GestureDetector 是手势识别的组件

    GestureDetector 是手势识别的组件,可以识别点击、双击、长按事件、拖动、缩放等手势。 #[http:...

  • iOS UIButton 拖动事件

    UIButton 添加添加拖动事件 也可以 如果不想拖动事件和按钮点击事件冲突,可以用UIPanGestureRe...

  • 拖动手势使用(UIPanGestureRecognizer)

    拖动手势创建 拖动事件(以下示例为上下拖动,上下左右拖动及滑行见注释部分) 参考文章:http://www.cnb...

  • swift学习控件创建

    图片加载和添加手势事件 按钮属性设置和点击事件

  • 手势锁与涂鸦画板

    gestureLock(手势锁)实现步骤 布局控件:背景控件和手势锁控件 手势锁控件:子控件:循环增加9个按钮,设...

  • 给按钮添加单击手势

    当给按钮添加一个单击手势之后,按钮会优先响应单击手势,而不会去响应按钮自己添加的action。 给UITableV...

  • iOS-UIView视图拖动

    UIView的视图拖动实现可以通过Touch的触摸事件来实现,也可以在UIView中添加拖动手势来实现. UITo...

  • iOS 点击事件与手势冲突

    最近做的一个需求,view上面有一个按钮,按钮添加了一个事件,view上添加了一个手势,当点击按钮时会和手势冲突,...

  • Android 之 Button (按钮)

    按钮: 效果: button点击事件: 第一种: 给xml中给button增加了android:onClick="...

网友评论

      本文标题:Vue+js 给按钮增加拖动手势事件

      本文链接:https://www.haomeiwen.com/subject/uuhyjdtx.html