uniapp点击预览图片,两种效果

背景:

在使用uniapp框架中,我们对图片的展示需要点击放大展示(单张);如果是多张图片,要支持左右滑动查看多张图片(多张)。

官网链接:点击跳转官网

一、单张,点击放大

代码:

<template>
<u--image :src="personPicUrl" width="100px" height="100px"
   @click="imgPreview(personPicUrl)"></u--image>
</template>

<script>
export default {
    name: "imageDetail",
    data() {
        return {
           personPicUrl: '',
      }
    },
    methods: {
        imgPreview(url) {
            uni.previewImage({
                urls: [url]
            })
        },
    },
</script>

核心代码:

//图片预览,封装方法   
imgPreview(url) {
            uni.previewImage({
                urls: [url]
            })
        },

二、多张,点击放大并左右滑动 

代码:

<template>
    <u--image v-for="(item, index) in imgsData" :key="index" 
        :src="item.img" 
        width="142rpx"
        height="104rpx" 
        radius="4" 
        @click="imgPreview(item.img)"
    >
    </u--image>
</template>

<script>
export default {
    name: "imageDetail",
    data() {
        return {
           imgsData: [],
      }
    },
    methods: {
        imgPreview(url) {
            let arr = []
            this.imgsData.forEach((item, index) => {
                if (item.img) {
                    arr.push(item.img)
                }
            })
            let index = arr.findIndex(value => value == url)
            uni.previewImage({
                current: index,
                urls: arr,
            });
        },
    },
</script>

核心代码:

//多张图片,可以左右滑动
imgPreview(url) {
            let arr = []
            this.imgsData.forEach((item, index) => {
                if (item.img) {
                    arr.push(item.img)
                }
            })
            let index = arr.findIndex(value => value == url)
            uni.previewImage({
                current: index,
                urls: arr,
            });
        }

 效果展示:

三、其它

长按图片,的效果

uni.previewImage({
                    current: index,
                    urls: arr,
                    longPressActions: {
                        itemList: ['发送给朋友', '保存图片', '收藏'],
                        success: function(data) {
                            console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
                        },
                        fail: function(err) {
                            console.log(err.errMsg);
                        }
                    }
                });

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值