Badge 徽标
徽标组件通常用于在头像、按钮等元素上显示小红点或者数量,以提醒用户有新消息或未读通知。
导入
js
import Badge from '@/components/display/Badge.vue'用法
基础用法
支持显示圆点、数字、文字,显示数字时支持最大值,超出最大值会以最大值+的形式显示。
vue
<template>
<FlexRow :gap="20" wrap>
<Badge>
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
<Badge content="1">
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
<Badge content="new">
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
<Badge content="新" border>
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
<Badge :content="1000" :maxCount="99" border>
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
</FlexRow>
</template>切换时有动画效果
通过 anim 属性设置切换时的动画效果。
vue
<template>
<FlexRow :gap="20" wrap>
<Badge :show="show" anim>
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
<Badge :show="show" anim :content="count">
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
<Badge :show="show" anim :content="count" :maxCount="99">
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
</FlexRow>
<Height :size="30" />
<FlexRow :gap="20" wrap>
<Button @click="show=!show">显示/隐藏</Button>
<Button @click="count--">数量-1</Button>
<Button @click="count++">数量+1</Button>
</FlexRow>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const show = ref(true)
const count = ref(1)
</script>不同颜色的徽标
通过 color 属性设置不同颜色的徽标。
vue
<template>
<FlexRow :gap="20" wrap>
<Badge>
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
<Badge color="primary">
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
<Badge color="success">
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
<Badge color="warning">
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
</FlexRow>
</template>不同位置的徽标
通过 position 属性设置徽标在父元素中的位置。
vue
<template>
<FlexRow :gap="20" wrap>
<Badge>
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
<Badge position="bottomLeft" color="primary">
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
<Badge position="topLeft" color="success">
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
<Badge position="bottomRight" color="warning">
<view style="width: 80rpx; height: 80rpx; background-color: #f5f5f5;"></view>
</Badge>
</FlexRow>
</template>独立展示
通过 standalone 属性设置徽标独立展示,不依附于其他元素。
vue
<template>
<FlexRow :gap="20" center>
<Badge standalone />
<Badge standalone color="primary" />
<Badge standalone color="success" />
<Badge standalone color="warning" />
<Badge standalone color="danger" />
</FlexRow>
<Height :size="20" />
<FlexRow :gap="20" center>
<Badge standalone />
<Badge standalone content="1" />
<Badge standalone content="new" border />
</FlexRow>
</template>API 参考
Badge
徽标组件。
Props
| 名称 | 说明 | 类型 | 必填 | 默认值 |
|---|---|---|---|---|
| show | 控制是否显示 | boolean | - | true |
| standalone | 独立显示,不依附于其他元素 | boolean | - | false |
| content | 徽标内容,支持数字或文本 | string/number | - | '' |
| maxCount | 徽标数字最大值,超过则显示 xx+ | number | - | - |
| color | 徽标颜色 | string | - | 'danger' |
| border | 是否增加描边 | boolean | - | false |
| borderWidth | 描边宽度 | number | - | 4 |
| borderColor | 描边颜色 | string | - | 'white' |
| position | 徽标在父级所处位置 | 'topRight' | 'topLeft' | 'bottomRight' | 'bottomLeft' | - | 'topRight' |
| anim | 是否在切换时有动画效果 | boolean | - | false |
| offset | 徽标定位偏移 | { x: number, y: number } | - | { x: 0, y: 0 } |
| badgeStyle | 徽标自定义样式 | object | - | {} |
| textStyle | 徽标文字自定义样式 | object | - | {} |
| containerStyle | 外层容器样式 | object | - | {} |
| badgeSize | 徽标无文字情况下的徽标大小 | number | - | 18 |
| fontSize | 字号 | number | - | 24 |
| radius | 圆角的大小 | number | - | 20 |
| paddingVertical | 徽标有文字情况下的垂直内边距 | number | - | 4 |
| paddingHorizontal | 徽标有文字情况下的水平内边距 | number | - | 8 |
| hiddenIfZero | 如果 content===0 是否隐藏红点 | boolean | - | true |
Slots
| 名称 | 说明 |
|---|---|
| default | 用于放置需要显示徽标的元素 |
主题变量
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| BadgeAnim | boolean | false | 是否默认显示动画 |
| BadgeBorder | boolean | false | 是否默认显示边框 |
| BadgeBorderWidth | number | 4 | 默认边框宽度 |
| BadgeBorderColor | string | 'white' | 默认边框颜色 |
| BadgePosition | string | 'topRight' | 默认徽标位置 |
| BadgeOffset | object | { x: 0, y: 0 } | 默认徽标偏移 |
| BadgeSize | number | 18 | 默认徽标大小 |
| BadgeRadius | number | 20 | 默认圆角大小 |
| BadgeFontSize | number | 24 | 默认文字大小 |
| BadgePaddingVertical | number | 4 | 默认垂直内边距 |
| BadgePaddingHorizontal | number | 8 | 默认水平内边距 |
| BadgeTextColor | string | 'white' | 徽标文字颜色 |