【uni-app】預覽圖片(單/多張)
阿新 • • 發佈:2021-01-09
官網:https://uniapp.dcloud.io/api/media/image?id=previewimage
uniapp 實現圖片預覽 單圖預覽和多圖預覽
關鍵點就是呼叫
uni.previewImage({
current: index,
urls: photoList
});
如果是單圖預覽,current對應就是0,urls:必須是單個某一條傳入。因為現在預覽一張圖片,你的current傳死就是0.所以urls也必須是字串形式
如果是多圖預覽,current,對應就是在迴圈圖片資料的索引,urls: 是陣列形式傳入。因為uni.previewImage 要根據 current的索引,來動態匹配urls裡面的資料
下面程式碼演示
單圖預覽
previewImg(photoImg) {
let imgsArray = [];
imgsArray[0] = photoImg;
uni.previewImage({
current: 0,
urls: imgsArray
});
}
多圖預覽
<template> <view> <view>預覽圖片</view> <view class="photosView"> <block v-for="(item, index) in photos" :key="index"> <view class="box"><image :src="item.src" mode="widthFix" @click="previewImage(index)"></image></view> </block> </view> </view> </template> <script> export default { data() { return { photos: [ { id: '1', src: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1592807923709&di=dda3e0b4d055c8dfaaaa9168c570983a&imgtype=0&src=http%3A%2F%2F00.minipic.eastday.com%2F20170331%2F20170331113141_98c8105e504ff71e68d59a6eaa30bd7e_5.jpeg' }, { id: '2', src: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1592807923711&di=6b8dfec17f7be8213ae23db6e1b7719a&imgtype=0&src=http%3A%2F%2F00.minipic.eastday.com%2F20170509%2F20170509200320_7dacf48b2bcc4b4ab7a0412b333ccb99_6.jpeg' }, { id: '3', src: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1592807858562&di=262d91cfb1292942a257d17973a4843c&imgtype=0&src=http%3A%2F%2F00.minipic.eastday.com%2F20170303%2F20170303094555_7851ce3d965701f3201d4df2dde56592_8.jpeg' }, { id: '4', src: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1592807923711&di=61c1d93f123d6039af37727812c64456&imgtype=0&src=http%3A%2F%2F00.minipic.eastday.com%2F20170318%2F20170318103604_eea6fa2eab8da83f770158463b7201f7_2.jpeg' } ] }; }, methods: { previewImage(index) { let photoList = this.photos.map(item => { return item.src; }); uni.previewImage({ current: index, urls: photoList }); } } }; </script> <style scoped lang="less"> * { margin: 0; padding: 0; } .photosView { display: flex; justify-content: space-between; flex-wrap: wrap; .box { width: 30%; image { width: 100%; height: 100%; } } } </style>