当前位置: 首页> 房产> 建材 > uniapp点击预览图片,两种效果

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

时间:2025/9/12 21:50:10来源:https://blog.csdn.net/weixin_45024453/article/details/141420218 浏览次数:0次

背景:

在使用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);}}});

关键字:uniapp点击预览图片,两种效果

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: