Vue移动端实现拖拽+吸边处理

在这里插入图片描述

<template>
    <div>
        <div ref="customerService"
             class="customer-service"
             @touchstart="touchstartHandle('customerService',$event)"
             @touchmove="touchmoveHandle('customerService',$event)"
             @touchend="end('customerService')"
             style="position:fixed">
        </div>
    </div>
</template>
<script>
export default {
    props: {

    },
    data () {
        return {
            touch: {
                initialPosition: {},
                movePostion: {}
            },
            element: {
                initialPosition: {},
                movePostion: {}
            },
            nowLeft: ''
        }
    },
    mounted () {

    },
    methods: {
        touchstartHandle (refName, e) {
            console.log(e)
            // 触摸开始
            let touchTarget = e.targetTouches[0];
            // 记录触摸点的坐标(初始位置)
            this.touch.initialPosition = {
                x: touchTarget.clientX,
                y: touchTarget.clientY
            }
            // 记录需要移动的元素坐标(初始位置)
            this.element.initialPosition = {
                x: this.$refs[refName].offsetLeft,
                y: this.$refs[refName].offsetTop
            }
        },
        touchmoveHandle (refName, e) {
            e.preventDefault();
            let touchTarget = e.targetTouches[0];
            // 根据初始touch位置计算移动距离:元素移动位置=元素初始位置+(光标移动后的位置-光标点击时的初始位置)
            let X = this.element.initialPosition.x + (touchTarget.clientX - this.touch.initialPosition.x);
            let Y = this.element.initialPosition.y + (touchTarget.clientY - this.touch.initialPosition.y);
            // 限制可移动距离,不超出可视区域
            let maxWidth = innerWidth - this.$refs[refName].offsetWidth;
            let maxHeight = innerHeight - this.$refs[refName].offsetHeight;
            
            X = X <= 0 ? 0 : X >= maxWidth ? maxWidth : X;
            Y = Y <= 0 ? 0 : Y >= maxHeight ? maxHeight : Y;
            console.log(X,Y)
			this.nowLeft = X;
            // 移动元素
            this.$refs[refName].style.left = X + 'px'
            this.$refs[refName].style.top = Y + 'px'
        },
        end (refName) {
            // 吸边处理
            let halfMaxWidth = (innerWidth - this.$refs[refName].offsetWidth) / 2;
            const dragDiv = this.$refs[refName];
            if (this.nowLeft> halfMaxWidth) {
                //   右吸边
                dragDiv.style.left =
                    document.documentElement.clientWidth -
                    parseInt(dragDiv.clientWidth) +
                    'px';
            }
            else {
                // 左吸边
                dragDiv.style.left = "0px";
            }
            dragDiv.style.top = dragDiv.offsetTop + 'px';
        }
    }
}
</script>
<style scoped lang="scss">
.customer-service {
    background-image: url("../assets/strawberry.jpg");
    background-size: contain;
    border: 2px dotted #f5a7a7;
    height: 100px;
    width: 100px;
    background-repeat: no-repeat;
}
</style>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Vue移动实现图片的双指放大缩小和,可以通过以下步骤进行操作。 首先,需要在Vue组件中引入相应的移动手势库,比如AlloyFinger或是基于它封装的vue-alloyfinger插件。这些手势库可以监听移动的触摸事件,方便实现手势操作。 其次,在组件的模板中需要渲染一张图片,并设置图片的初始宽度和高度。可以通过绑定样式属性的方式,将图片的宽度和高度与组件中的data数据绑定起来。 然后,需要为图片绑定手势事件,并在对应的方法中实现双指放大缩小和的逻辑。比如,可以监听双指缩放事件,在事件处理函数中根据手指的位置和缩放比例来更新图片的宽度和高度。可以监听事件,在事件处理函数中根据手指的移动距离来更新图片的位置。 最后,还可以添加一些边界判断,比如设置图片的最大和最小缩放比例,防止图片过小或过大。还可以添加过渡动画,使操作更加平滑。 需要注意的是,双指放大缩小和实现需要一定的数学计算,比如计算手指的距离和角度,或是计算图片的偏移量等。因此,在实现过程中需要对数学计算有一定的了解。 综上所述,通过Vue移动手势库,我们可以很方便地实现图片的双指放大缩小和功能。通过监听手势事件,并在事件处理函数中更新图片的属性和位置,可以实现用户友好的图片操作效果。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值