当前位置: 首页 > news >正文

uniapp实现图片懒加载 封装组件

想要的效果就是窗口滑动到哪里,哪里的图片进行展示 主要原理使用IntersectionObserver

<template><view><image @error="HandlerError" :style="imgStyle" :src="imageSrc" :id="randomId" :mode="mode" class="music-img" /></view>
</template>
<script setup lang="ts">
import { uuid } from '@/utils/index'const instance = getCurrentInstance()
let observer2: any = null
const randomId = ref<string>('')
randomId.value = uuid()
type Props = {src: stringloadingSrc: stringimgStyle: anymode: stringclassName: string
}
const props = defineProps<Props>()
let src = computed(() => {return props.src || ''
})
let loadingSrc = computed(() => {return props.loadingSrc || ''
})
let imgStyle = computed(() => {return props.imgStyle || { width: '200rpx' }
})
let mode = computed(() => {return props.mode || ''
})let imageSrc = ref<string>('')imageSrc.value = loadingSrc.valueconst HandlerError = () => {}
onMounted(() => {if (imageSrc.value == loadingSrc.value) {// #ifdef APP || H5const observer = new IntersectionObserver((entries) => {if (entries[0].intersectionRatio > 0) { //进入页面的占比>0 就认为要显示const img = entries[0].targetimageSrc.value = src.valueobserver.unobserve(img)}},{root: null,rootMargin: '0px',threshold: 0.1})const img: Element | null = document.getElementById(`${randomId.value}`)if (img) {observer.observe(img)}// #endif// #ifndef APP || H5observer2 = uni.createIntersectionObserver(instance).relativeToViewport()observer2.observe('.music-img', (res) => {if (res.intersectionRatio > 0) {imageSrc.value = src.value}})// #endif}
})
onUnmounted(() => {// #ifndef APP || H5if (observer2) {observer2.disconnect()}// #endif
})
</script><style></style>

http://www.lryc.cn/news/391863.html

相关文章:

  • 持续交付:自动化测试与发布流程的变革
  • VBA常用的字符串内置函数
  • 大数据面试题之Spark(7)
  • AI绘画 Stable Diffusion图像的脸部细节控制——采样器全解析
  • liunx离线安装Firefox
  • UNet进行病理图像分割
  • 初二数学基础差从哪开始补?附深度解析!
  • 【C语言】return 关键字
  • 华为机试HJ13句子逆序
  • 代码随想录day40 动态规划(5)
  • FFmpeg 命令行 音视频格式转换
  • Jmeter使用JSON Extractor提取多个变量
  • c++ 设计模式 的课本范例(下)
  • 结合数据索引结构看SQL的真实执行过程
  • spark shuffle——shuffle管理
  • HTMLCSS(入门)
  • 富格林:曝光可信策略制止亏损
  • Android --- Service
  • Vue3从入门到精通(三)
  • 【FreeRTOS】同步与互斥通信-有缺陷的互斥案例
  • Docker 安装 Python
  • 外泌体相关基因肝癌临床模型预测——2-3分纯生信文章复现——4.预后相关外泌体基因确定单因素cox回归(2)
  • C++: Map数组的遍历
  • 【Windows】Bootstrap Studio(网页设计)软件介绍及安装步骤
  • 二维舵机颜色追踪,使用树莓派+opencv+usb摄像头+两个舵机实现颜色追踪,采用pid调控
  • c进阶篇(四):内存函数
  • 新手入门:无服务器函数和FaaS简介
  • 基于Transformer的端到端的目标检测 | 读论文
  • 6.8应用进程跨网络通信
  • redis布隆过滤器原理及应用场景