暗黑模式
Badge 徽标(数字角标)
手机扫码预览
01:50
组件名:rice-badge
在元素上显示徽标数字或小红点。
注意
由于 uniappx 中的 app 端 display 只支持 flex ,所以该组件需要在 flex 布局下进行使用,或者手动给Badge 组件的父元素设置宽度。否则徽标显示的位置可能不正确。 如果徽标显示不全,需要给 Badge 组件的父元素设置 overflow 为 visible。 具体使用方式见demo。
平台兼容性
uni-app x
| Android | iOS | 鸿蒙Next | 微信小程序 | h5 |
|---|---|---|---|---|
| √ | √ | √ | √ | √ |
基础用法
设置 value 属性后,Badge 会在子元素的右上角显示对应的徽标,value 值可以是字符串也可以是数字。也可以通过 dot 属性来显示小红点。
html
<rice-badge :value="5">
<view class="block"></view>
</rice-badge>
<rice-badge value="Hot">
<view class="block"></view>
</rice-badge>
<rice-badge :value="999" is-dot>
<view class="block"></view>
</rice-badge>css
.block {
height: 42px;
width: 42px;
background-color: var(--demo-block-color-3);
border-radius: 4px;
}搭配组件
Badge 可以搭配组件使用
html
<rice-badge :value="5">
<rice-avatar src="/static/images/avatar.png" />
</rice-badge>
<rice-badge value="hot">
<rice-button type="primary" size="small" text="热门商品" @click="onClick"></rice-button>
</rice-badge>
<rice-badge :value="100" :max="99">
<rice-icon name="star-fill" size="34px" color="#d54941"/>
</rice-badge>最大值
通过 max 属性可以设置最大值,超过该值后会显示 {max}+ 注意:vulue 和 max 的值都为number 类型时才有效。
html
<rice-badge :value="10" :max="9">
<view class="block"></view>
</rice-badge>
<rice-badge :value="21" :max="20">
<view class="block"></view>
</rice-badge>
<rice-badge :value="100" :max="99">
<view class="block"></view>
</rice-badge>主题色
可以通过 tyep 属性设置 徽标的主题,支持 primary success warning 默认为 error
html
<rice-badge :value="5" type="primary">
<view class="block"></view>
</rice-badge>
<rice-badge :value="10" type="success" :max="9">
<view class="block"></view>
</rice-badge>
<rice-badge :value="39" type="warning" is-dot>
<view class="block"></view>
</rice-badge>自定义颜色
- 通过
bg-color可以自定义徽标的背景色 - 通过
color自定义文字的颜色
html
<rice-badge :value="9" bg-color="#845ec2">
<view class="block"></view>
</rice-badge>
<rice-badge :value="29" bg-color="#dcf2ff" color="#007aff" :max="9">
<view class="block"></view>
</rice-badge>
<rice-badge is-dot bg-color="#ac73e1">
<view class="block"></view>
</rice-badge>自定义位置
通过 position 属性可以设置徽标展示的位置,默认为 top-right,可选 top-leftbottom-left bottom-right。
html
<rice-badge :value="5" position="bottom-right">
<view class="block"></view>
</rice-badge>
<rice-badge :value="10" position="top-left">
<view class="block"></view>
</rice-badge>
<rice-badge value="Hot" position="bottom-left">
<view class="block"></view>
</rice-badge>自定义偏移量
通过 offset 属性可以设置徽标的偏移量,第一个值为水平向右的偏移量,第二个值为垂直向下的偏移量,可以简单的理解为徽标相对于当前位置要向右移动多少距离,要向下移动多少距离。数组值可以是字符串,也可以是数字。
html
<rice-badge :value="5" position="bottom-right" :offset="[-10,-10]">
<view class="block"></view>
</rice-badge>独立展示
徽标支持单独使用
html
<rice-badge :value="5"></rice-badge>
<rice-badge :value="100" :max="99" type="primary"></rice-badge>
<rice-badge is-dot></rice-badge>API
Props
| 属性名 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| value | String|Number | 显示值,is-dot 为 false时生效;为0时且 show-zero 为 false 时隐藏 | - |
| max | Number | 最大值,大于 max 时显示为 ${max}+。只有value 为数字类型时才起作用 | - |
| is-dot | Boolean | 是否显示小圆点 | false |
| hidden | Boolean | 是否隐藏 Badge | false |
| type | String | 主题类型 可选值:primary、success、warning | error |
| show-zero | Boolean | 数值为0时是否显示Badge | true |
| bg-color | String | 背景颜色,优先级高级 type | - |
| color | String | 文字颜色 | - |
| font-size | String|Number | 文字大小 | - |
| position | String | Badge 的位置,默认 top-right,可选值:top-right、top-left、bottom-left、bottom-right | top-right |
| offset | Array | 偏移量,数组的两项分别对应水平向右和垂直向下的偏移量 | - |
| absolute | Boolean | 是否绝对定位 | - |
| badge-style | Object | 自定义Badge样式 | - |
Slot
| 名称 | 说明 |
|---|---|
| default | 徽标包裹的子元素 |
| content | 自定义徽标的内容 |
类型定义
组件导出如下类型
ts
const badgeRef=ref<RiceBadgeComponentPublicInstance|null>(null)