Skip to content

Skeleton 骨架屏 ​

uvx-skeleton 在接口数据加载期间展示占位结构,支持线条、头像、横向并列、自定义占位块和垂直间距,加载完成后通过默认插槽显示真实内容。

平台兼容性 ​

AndroidiOS鸿蒙H5微信
3.9+4.11+4.61+4.0+4.41+

小程序端仅支持微信。

平台能力 ​

能力AndroidiOS鸿蒙H5微信
line、avatar、flex、custom、gap 配置√√√√√
默认插槽√√√√√
animate 原生动画√√√√√
暗色主题变量√√√√√

使用须知

  1. loading 默认为 true。为 false 时组件只渲染默认插槽内容。
  2. skeleton 必须是数组;数组项可以是配置对象或数字。数字表示垂直间距,数字单位为 rpx。
  3. style 支持 CSS 字符串、UTSJSONObject 或同类型数组。样式数组中使用空字符串表示该项不覆盖默认样式,不使用 null。
  4. num 未传时为 1,传入 0 或负数时不生成节点;gap 传入 0 或空字符串时不生成重复节点之间的间距。
  5. round 只影响线条和基础占位节点的圆角,头像始终保持圆形;custom 节点的圆角由自身 style 决定。
  6. animate 使用组件内部动画引擎驱动横向扫光高亮,节点较多的复杂页面建议关闭动画以降低原生帧更新开销。
  7. 骨架屏不会测量真实内容高度,复杂布局应通过 style 显式设置宽高。

基础使用 ​

使用 type 和 num 创建多条线形占位内容。gap 控制重复节点之间的间距。

vue
<template>
   <uvx-skeleton
      :loading="loading"
      :skeleton="skeleton"
   >
      <uvx-text text="数据加载完成" type="content"></uvx-text>
   </uvx-skeleton>
</template>

<script lang="uts" setup>
import type { Config } from "@/uni_modules/uvx-ui/components/uvx-skeleton/types/index.uts";

const loading = ref(true);
const skeleton = ref<Config>([
   { type: "line", num: 3, gap: "20rpx" },
]);
</script>

头像占位 ​

avatar 默认生成圆形占位块,可通过样式调整尺寸。avatar--sm 和 avatar--lg 类名也可以通过自定义 uClass 扩展样式。

vue
<template>
   <uvx-skeleton
      :skeleton="avatarSkeleton"
   ></uvx-skeleton>
</template>

<script lang="uts" setup>
import type { Config } from "@/uni_modules/uvx-ui/components/uvx-skeleton/types/index.uts";

const avatarSkeleton: Config = [
   { type: "avatar", style: "width: 50rpx; height: 50rpx;" },
];
</script>

横向并列布局 ​

flex 使用 children 声明横向子分组。普通子分组会自动占据剩余空间,头像和自定义块保持自身尺寸。

vue
<template>
   <uvx-skeleton
      :skeleton="skeleton"
      :animate="true"
   ></uvx-skeleton>
</template>

<script lang="uts" setup>
import type { Config } from "@/uni_modules/uvx-ui/components/uvx-skeleton/types/index.uts";

const skeleton: Config = [
   {
      type: "flex",
      children: [
         { type: "avatar", style: "margin-right: 10rpx;" },
         { type: "line", num: 3, gap: "20rpx" },
      ],
   },
];
</script>

自定义样式 ​

style 可以传入 CSS 字符串;当同一类型通过 num 重复生成时,传入样式数组为每一个节点指定样式。数组长度不足的节点使用默认样式。

vue
<template>
   <uvx-skeleton :skeleton="skeleton"></uvx-skeleton>
</template>

<script lang="uts" setup>
import type { Config } from "@/uni_modules/uvx-ui/components/uvx-skeleton/types/index.uts";

const skeleton: Config = [
   {
      type: "line",
      num: 4,
      style: [
         "width: 200rpx;",
         "",
         "",
         "width: 500rpx;",
      ],
   },
];
</script>

自定义占位块和间距 ​

custom 不设置默认尺寸,适合图片、卡片等需要自定义宽高的区域;数字配置项表示垂直间距。

vue
<template>
   <uvx-skeleton :skeleton="skeleton"></uvx-skeleton>
</template>

<script lang="uts" setup>
import type { Config } from "@/uni_modules/uvx-ui/components/uvx-skeleton/types/index.uts";

const skeleton: Config = [
   { type: "custom", style: "width: 100%; height: 160rpx;" },
   30,
   { type: "line", num: 2, gap: "20rpx" },
];
</script>

圆角和动画 ​

round 让线条和普通占位块使用胶囊形圆角,animate 控制占位节点的横向扫光动画。

vue
<template>
   <uvx-skeleton
      :skeleton="skeleton"
      :round="true"
      :animate="false"
   ></uvx-skeleton>
</template>

<script lang="uts" setup>
import type { Config } from "@/uni_modules/uvx-ui/components/uvx-skeleton/types/index.uts";

const skeleton: Config = [
   { type: "line", num: 3 },
];
</script>

自定义根节点样式 ​

uStyle 设置根节点 CSS 字符串,uClass 追加外部样式类。

vue
<template>
   <uvx-skeleton
      :skeleton="rootSkeleton"
      u-class="profile-skeleton"
      u-style="padding: 20rpx;"
   ></uvx-skeleton>
</template>

<script lang="uts" setup>
import type { Config } from "@/uni_modules/uvx-ui/components/uvx-skeleton/types/index.uts";

const rootSkeleton: Config = [
   { type: "line", num: 2 },
];
</script>

API ​

Props ​

属性说明类型默认值平台
loading是否显示骨架屏,关闭后显示默认插槽booleantrue全部
skeleton骨架屏配置数组,数组项为配置对象或数字间距Config[]全部
animate是否开启动画booleantrue全部
round是否使用胶囊形圆角booleanfalse全部
u-style根节点 CSS 样式字符串string""全部
u-class根节点外部样式类string""全部

Config ​

字段说明类型默认值
type节点类型:line、avatar、flex、custom、gapNodeTypeline
num重复生成数量,非正数不生成节点number未设置时内部按 1 处理
style节点样式或按重复节点下标指定的样式数组string | UTSJSONObject | Array<string | UTSJSONObject>""
gap重复节点之间的间距,数字单位为 rpxstring | number未设置时内部按 "20rpx" 处理
childrenflex 的横向子配置Array<Item>[]
heightgap 类型节点的高度,数字单位为 rpxstring | number0

数字配置项直接表示垂直间距,数字单位为 rpx。

Slots ​

插槽名说明平台
defaultloading=false 时显示的真实内容全部

Events ​

组件没有自定义事件。

参考 ​

uvx-ui · uni-app x 组件库