美文网首页
vue 实现拖拽案例 uniapp

vue 实现拖拽案例 uniapp

作者: Feng_Du | 来源:发表于2022-03-01 21:28 被阅读0次

直接复制运行,简单易懂。

<template>
    <view class="content">
        <image class="logo" @touchstart='WraptouchStart' @touchmove='WraptouchMove'
            :style="{transform:`translate(${imginfo.x}px,${imginfo.y}px) scale(1)`}" src="/static/logo.png"></image>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title: 'Hello',
                zoom: 0.5,
                imginfo: {
                    lx: 0,
                    ly: 0,
                    x: 0,
                    y: 0
                }
            }
        },
        onLoad() {

        },
        methods: {
            WraptouchStart(e) {
                this.imginfo.lx = e.touches[0].clientX;
                this.imginfo.ly = e.touches[0].clientY;
            },
            WraptouchMove(e) {
                this.imginfo.x += (e.touches[0].clientX - this.imginfo.lx) / this.zoom
                this.imginfo.y += (e.touches[0].clientY - this.imginfo.ly) / this.zoom
                this.imginfo.lx = e.touches[0].clientX;
                this.imginfo.ly = e.touches[0].clientY;
            }
        }
    }
</script>

<style>
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        zoom: 0.5;
    }

    .logo {
        height: 200rpx;
        width: 200rpx;
        margin-top: 200rpx;
        margin-left: auto;
        margin-right: auto;
        margin-bottom: 50rpx;
    }
</style>

ps:如果父级元素有用到缩放 /zoom对应的缩放比就行

相关文章

网友评论

      本文标题:vue 实现拖拽案例 uniapp

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