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: string
  loadingSrc: string
  imgStyle: any
  mode: string
  className: 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.value

const HandlerError = () => {}
onMounted(() => {
  if (imageSrc.value == loadingSrc.value) {
    // #ifdef APP || H5
    const observer = new IntersectionObserver(
      (entries) => {
        if (entries[0].intersectionRatio > 0) { //进入页面的占比>0 就认为要显示
          const img = entries[0].target
          imageSrc.value = src.value
          observer.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 || H5
    observer2 = uni.createIntersectionObserver(instance).relativeToViewport()
    observer2.observe('.music-img', (res) => {
      if (res.intersectionRatio > 0) {
        imageSrc.value = src.value
      }
    })
    // #endif
  }
})
onUnmounted(() => {
  // #ifndef APP || H5
  if (observer2) {
    observer2.disconnect()
  }
  // #endif
})
</script>

<style></style>

相关推荐

  1. uniapp实现图片 封装组件

    2024-07-10 16:00:02       13 阅读
  2. 关于如何实现图片

    2024-07-10 16:00:02       47 阅读
  3. Vue3实现图片

    2024-07-10 16:00:02       27 阅读
  4. IntersectionObserver实现图片

    2024-07-10 16:00:02       18 阅读
  5. 【Vue】图片实现

    2024-07-10 16:00:02       16 阅读
  6. Vue3图片封装自定义指令

    2024-07-10 16:00:02       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 16:00:02       5 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 16:00:02       5 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 16:00:02       4 阅读
  4. Python语言-面向对象

    2024-07-10 16:00:02       5 阅读

热门阅读

  1. 有关区块链的一些数学知识储备

    2024-07-10 16:00:02       6 阅读
  2. MICCAI 2023 List of Papers

    2024-07-10 16:00:02       8 阅读
  3. uniapp如何发送websocket请求

    2024-07-10 16:00:02       10 阅读
  4. react

    react

    2024-07-10 16:00:02      8 阅读
  5. 光通信领域常见的会议和期刊总结

    2024-07-10 16:00:02       10 阅读
  6. uniapp上传文件并获取上传进度

    2024-07-10 16:00:02       11 阅读
  7. C++继承

    C++继承

    2024-07-10 16:00:02      8 阅读
  8. ArcGIS Pro SDK (八)地理数据库 2 定义

    2024-07-10 16:00:02       10 阅读