📚
note
Blog
  • Initial page
  • JS-notes
    • 使用浏览器书签执行js代码
    • JSON.stringify的小技巧
    • Fisher–Yates shuffle洗牌算法
    • 打印页面
  • Web-notes
    • 在网页中引入在线字体
  • Uniapp-notes
    • swiper-item高度自适应
    • 微信小程序的图片预览兼容性处理
  • VUE-notes
    • vue-note
    • vue3-note
  • VPS-notes
    • CentOs7笔记
    • ssh小记
    • Ubuntu笔记
    • vps安全相关
    • [Google Drive笔记](VPS-notes/Google Drive笔记.md)
  • TypeScript-notes
    • ts热编译项目
    • TypeScript笔记
    • js项目转ts
  • Python-notes
    • Python爬虫笔记
    • Python笔记
  • PHP-notes
    • php笔记
    • php+redis
    • php-codeIgniter
    • php抽奖算法
    • Laravel笔记
  • Mobile-notes
    • 移动端常用样式及兼容相关
  • Linux-notes
    • linux常用指令
  • Game-notes
    • Minecraft-server
  • TelegramBot-notes
    • tg-bot小记
  • Windows-notes
    • window-note
    • node-note
    • WSL-note
  • RaspberryPi-notes
    • RaspberryPi-note
    • 其他玩法
    • Openwrt
    • Ubuntu安装指南
  • Phone-notes
    • ZenFone6-note
  • Cocos-notes
    • Cocos-note
  • Network-notes
    • 单线复用
  • Other-notes
    • 国际化地域标识码
Powered by GitBook
On this page

Was this helpful?

  1. Uniapp-notes

微信小程序的图片预览兼容性处理

在开发时发现,使用uniapp的预览图片方法(uni.previewImage)在微信小程序上进行图片预览时,会发现预览窗口黑屏,没法正常显示图片。在网上搜索资料发现需要做以下设置

<view class="list-item" v-for="(item,idx) in list" :key="idx">
	<u-image @click.native.prevent="previewImage($event,idx)" :data-src="item" mode="aspectFit" width="200rpx" height="200rpx" border-radius="24rpx" :src="item">
	</u-image>
</view>

在绑定点击事件时,需要传递两个值,一个是事件本身属性,一个是图片数组的索引值。需要注意的是在图片组件需要传一个自定义参数:data-src="item"这个参数是单张图片的地址。

previewImage(e, idx) {
	// #ifdef MP-WEIXIN
	const current = e.target.dataset.src
	uni.previewImage({
		urls: this.picList,
		current: current
	})
	// #endif
	// #ifndef MP-WEIXIN
	uni.previewImage({
		urls: this.picList,
		current: idx,
	})
	// #endif
}

在预览图片的方法里,根据运行环境是否为微信小程序进行判断,分别给current传递不同的参数,这样就能在微信小程序中正确的显示图片预览了。

Last updated 2 years ago

Was this helpful?