uniapp兼容h5、app、微信小程序图片懒加载

<view class="recommend_item_image">
	<view class="img_item" v-for="(eImage,ei) in item.imageList" :key="ei">
		<image @click.stop="clickImg(eImage)" :src="(item.show ? eImage.imageItem : '')" :class="[item.show ? 'active': '']"></image>
	</view>
</view>
onPageScroll() { // 滚动事件 this.showImg()
			uni.$u.throttle(this.showImg, 500)
		},
		onReady() {
			let that = this
			uni.getSystemInfo({
				success(res) {
					that.windowHeight = res.windowHeight;
				}
			});
		},
showImg() {
				let that = this
				// #ifndef H5
				uni.createSelectorQuery().selectAll('.recommend_commodity_item').boundingClientRect((ret) => {
					ret.forEach((item, index) => {
						if (item.top <= that.windowHeight) {
							that.$set(that.recommendData[index], 'show', true)
						}
					})
				}).exec()
				// #endif
				// #ifdef H5
				const observer = new IntersectionObserver((entries, observer) => {
					entries.forEach((entry, index) => {
						if (entry.isIntersecting) {
							that.$set(that.recommendData[index], 'show', true)
							observer.unobserve(entry.target);
						}
					});
				});

				const lazyImages = document.querySelectorAll('.recommend_commodity_item');
				lazyImages.forEach((img) => {
					observer.observe(img);
				});
				// #endif
			},
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 H5 页面中跳转到微信小程序,需要使用微信提供的 JS-SDK。 首先,在 H5 页面中引入微信 JS-SDK: ```html <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> ``` 然后,在页面完成后,初始化 JS-SDK: ```javascript wx.config({ // 这里需要填写微信公众号的 appid appId: 'your_app_id', // 这里需要填写当前页面的完整 URL,不包括 # 及其后面部分 // 例如:http://www.example.com/path/to/page // 注意:JS-SDK 要求 URL 必须是经过encodeURIComponent编码的 // 所以需要使用 encodeURIComponent 方法进行编码 // 下面的示例假设当前页面的 URL 是 http://www.example.com/path/to/page#abc // 则需要编码的 URL 是 http%3A%2F%2Fwww.example.com%2Fpath%2Fto%2Fpage // 注意:如果当前页面的 URL 有参数,则需要把参数也编码进去 // 例如:http://www.example.com/path/to/page?foo=bar&baz=qux // 则需要编码的 URL 是 http%3A%2F%2Fwww.example.com%2Fpath%2Fto%2Fpage%3Ffoo%3Dbar%26baz%3Dqux // 如果不知道如何编码 URL,可以使用 encodeURIComponent 方法 // 例如:encodeURIComponent('http://www.example.com/path/to/page#abc') // 输出:http%3A%2F%2Fwww.example.com%2Fpath%2Fto%2Fpage%23abc // 可以使用浏览器的 Console 进行测试 // 注意:下面的示例中,URL 部分需要替换成实际的 URL // 示例中的 appid 和 URL 都是假的,请替换成实际的 appid 和 URL // 注意:wx.config 方法需要传入一个回调函数,用于在配置完成后执行 // 回调函数中的代码可以调用 wx.checkJsApi 方法检查当前环境是否支持 JS-SDK 的相关 API // 如果支持,则可以在回调函数中执行其他操作,例如打开微信小程序 // 如果不支持,则需要提示用户在微信客户端打开页面 jsApiList: ['checkJsApi'] // 这里需要填写微信公众号的 secret // 然后调用 wx.ready 方法,在回调函数中执行其他操作 // 例如打开微信小程序 }); ``` 最后,通过调用 wx.miniProgram.navigateTo 方法打开微信小程序: ```javascript wx.miniProgram.navigateTo({ // 这里需要填写要跳转的小程序的 appid 和页面路径 // 例如:appid: 'wx1234567890', path: 'pages/index/index' appid: 'your_mini_program_appid', path: 'your_mini_program_page_path' }); ``` 需要注意的是,以上代码仅适用于在微信浏览器中打开的 H5 页面。如果在其他浏览器中打开,或使用微信开发者工具预览,则无法跳转到微信小程序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值