Skip to content

xtf-lazyload

组件说明

xtf-lazyload 是图片懒加载组件,使用 IntersectionObserver 实现图片进入视口时才加载。支持占位图、骨架屏、加载失败重试和自定义尺寸,适用于长列表图片和商品图片等场景。


基础用法

1. 基础用法

通过 src 设置图片地址,通过 threshold 设置提前加载距离:

vue
<template>
  <scroll-view scroll-y style="height: 100vh">
    <xtf-lazyload
      v-for="item in imageList"
      :key="item.id"
      :src="item.url"
      width="100%"
      :height="400"
      radius="20"
    />
  </scroll-view>
</template>

<script>
export default {
  data() {
    return {
      imageList: [
        { id: 1, url: '/static/img1.jpg' },
        { id: 2, url: '/static/img2.jpg' },
        { id: 3, url: '/static/img3.jpg' }
      ]
    }
  }
}
</script>

2. 带占位图

通过 placeholder 设置占位图:

vue
<template>
  <xtf-lazyload
    src="/static/large-image.jpg"
    placeholder="/static/placeholder.png"
    width="100%"
    :height="500"
  />
</template>

<script>
export default {
  data() {
    return {}
  }
}
</script>

3. 自定义加载与错误状态

通过具名插槽替换内置加载和错误内容,并使用 error 事件记录失败原因:

vue
<template>
  <xtf-lazyload src="/static/missing.jpg" :height="240" @error="handleError">
    <template #loading><xtf-loading text="正在加载图片" /></template>
    <template #error><xtf-text text="图片暂不可用" color="secondary" align="center" /></template>
  </xtf-lazyload>
</template>

<script>
export default {
  methods: {
    handleError(event) {
      console.log('图片加载失败:', event)
    }
  }
}
</script>

全部属性

属性类型默认值作用描述适用范围
srcString''图片地址图片
placeholderString''占位图地址占位
errorImgString''错误占位图错误
thresholdString | Number100提前加载距离(rpx)触发
fitString'aspectFill'填充模式模式
altString''图片描述无障碍
widthString | Number'100%'宽度尺寸
heightString | Number300高度尺寸
radiusString | Number'var(--xtf-radius-md)'圆角样式
roundBooleanfalse是否圆形形状
customClassString''自定义类名样式覆盖
customStyleString | Object''自定义样式动态样式覆盖

事件

事件名称触发时机回调参数参数说明
load图片加载完成时触发(event)原生 image 的加载事件
error图片加载失败时触发(event)原生 image 的错误事件

插槽

插槽名称说明
loading自定义加载中状态
error自定义错误状态

主题说明

  • 加载中使用骨架屏闪烁动画
  • 错误状态使用 xtf-icon 组件

如需全局调整主题,请在 xtf-config-provider 或主题配置中覆盖相关 CSS 变量。

MIT Licensed