前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >uniapp 图片拖拽滑动或双指放大缩小

uniapp 图片拖拽滑动或双指放大缩小

作者头像
李维亮
发布2023-05-09 16:44:34
3K0
发布2023-05-09 16:44:34
举报
文章被收录于专栏:李维亮的博客李维亮的博客

uniapp 原生自带,偷懒搬个demo 传送门

代码语言:javascript
复制
<template>
    <view class="page-body">
        <page-head title="movable-view,可拖动视图"></page-head>
        <view class="uni-padding-wrap uni-common-mt">
            <view class="uni-title uni-common-mt">
                示例 1
                <text>\nmovable-view 区域小于 movable-area</text>
            </view>
            <movable-area>
                <movable-view :x="x" :y="y" direction="all" @change="onChange">text</movable-view>
            </movable-area>
            <view @tap="tap" class="uni-link uni-center uni-common-mt">
                点击这里移动至 (30px, 30px)
            </view>
            <view class="uni-title uni-common-mt">
                示例 2
                <text>\nmovable-view区域大于movable-area</text>
            </view>
            <movable-area>
                <movable-view class="max" direction="all">text</movable-view>
            </movable-area>
            <view class="uni-title uni-common-mt">
                示例 3
                <text>\n只可以横向移动</text>
            </view>
            <movable-area>
                <movable-view direction="horizontal">text</movable-view>
            </movable-area>
            <view class="uni-title uni-common-mt">
                示例 4
                <text>\n只可以纵向移动</text>
            </view>
            <movable-area>
                <movable-view direction="vertical">text</movable-view>
            </movable-area>
            <view class="uni-title uni-common-mt">
                示例 5
                <text>\n可超出边界</text>
            </view>
            <movable-area>
                <movable-view direction="all" out-of-bounds>text</movable-view>
            </movable-area>
            <view class="uni-title uni-common-mt">
                示例 6
                <text>\n带有惯性</text>
            </view>
            <movable-area>
                <movable-view direction="all" inertia>text</movable-view>
            </movable-area>
            <view class="uni-title uni-common-mt">
                示例 7
                <text>\n可放缩</text>
            </view>
            <movable-area scale-area>
                <movable-view direction="all" @scale="onScale" scale scale-min="0.5" scale-max="4" :scale-value="scale">text</movable-view>
            </movable-area>
            <view @tap="tap2" class="uni-link uni-center uni-common-mt" style="padding-bottom:80rpx;">
                点击这里放大3倍
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                x: 0,
                y: 0,
                scale: 2,
                old: {
                    x: 0,
                    y: 0,
                    scale: 2
                }
            }
        },
        methods: {
            tap: function(e) {
                // 解决view层不同步的问题
                this.x = this.old.x
                this.y = this.old.y
                this.$nextTick(function() {
                    this.x = 30
                    this.y = 30
                })
            },
            tap2() {
                // 解决view层不同步的问题
                this.scale = this.old.scale
                this.scale = this.old.scale
                this.$nextTick(function() {
                    this.scale = 3
                })
            },
            onChange: function(e) {
                this.old.x = e.detail.x
                this.old.y = e.detail.y
            },
            onScale: function(e) {
                this.old.scale = e.detail.scale
            }
        }
    }
</script>

<style>
    movable-view {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 150rpx;
        width: 150rpx;
        background-color: #007AFF;
        color: #fff;
    }
    movable-area {
        height: 300rpx;
        width: 100%;
        background-color: #D8D8D8;
        overflow: hidden;
    }
    .max {
        width:500rpx;
        height: 500rpx;
    }
</style>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-05-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档