Skip to content

Badge 徽标数

该组件一般用于图标右上角显示未读的消息数量,提示用户点击,有圆点和圆包含文字两种形式。

平台差异说明

App(vue)App(nvue)H5微信小程序

基本使用

  • 通过value参数定义徽标内容
  • 通过type设置主题。重申一次,sun-uni中,所有组件的type参数都只有5个固定的可选值,分别是primary(蓝色-主色),warning(黄色-警告), error(红色-错误),success(绿色-成功),info(灰色-信息)

注意

此组件内部默认为absulote绝对定位,所以需要给badge父组件(元素)设置position: relative相对定位, 再通过调整offset偏移值(数组,两个元素,第一个元素为top值,第二个元素为right值,单位rpx,可为负值,如"[-10, -10]")设置到合适的位置即可。 如果不需要组件内容默认的自动绝对定位,设置absolute参数为false即可。

html
<su-badge type="error" :value="7"></su-badge>

徽标数显示方式

  • numberType参数默认为overflow,会根据max字段判断,超出显示${max}+
  • ellipsis会根据max判断,超出显示${max}...
  • limit会依据1000作为判断条件,超出1000,显示${value/1000}K,比如2.2k、3.34w,最多保留2位小数
html
<su-badge :value="5132" numberType="ellipsis"></su-badge>
<su-badge :value="1011" numberType="overflow"></su-badge>
<su-badge :value="1500" numberType="limit"></su-badge>
<su-badge :value="45187" numberType="limit"></su-badge>

设置徽标的类型为一个圆点

通过is-dot参数设置,该形式组件没有内容,只显示一个圆点

html
<su-badge :is-dot="true" type="success"></su-badge>

反转色

通过inverted参数设置是否反转背景和字体颜色

html
<su-badge :inverted="true" type="success"></su-badge>

自定义徽标样式

可以通过custom-style自定义样式

html
<su-badge type="green" :custom-style="customStyle"></su-badge>

<script lang="ts" setup>
  const customStyle = {
    'background-color': 'blue',
    color: 'white'
  }
</script>

示例源码

点击可以查看 右侧演示页面的源码

API

Props

参数说明类型默认值可选值
value/modelValue展示的数字String | Number--
is-dot不展示数字,只有一个小点Booleanfalsetrue
absolute组件是否绝对定位,为true时,offset参数才有效Booleantruefalse
type使用预设的背景颜色Stringerrorsuccess / primary / warning / info
offset设置badge的位置偏移,格式为 [x, y],也即设置的为topright的值,单位rpx。absolutetrue时有效Array[20, 20]-
color字体颜色String#ffffff-
bgColor背景颜色,优先级比type高,如设置,type参数会失效String--
show是否显示, 默认显示Booleantruetrue / false
max最大值,超过最大值会显示 '{max}+'String | Number999-
show-zero当数值为 0 时,是否展示 BadgeBooleanfalsetrue / false
shape徽标形状,circle-四角均为圆角,horn-左下角为直角circle | horncirclecircle / horn
numberType设置数字的显示方式overflow | ellipsis | limitoverflowoverflow / ellipsis / limit
inverted是否反转背景和字体颜色Booleanfalsetrue / false

Released under the MIT License.

Released under the MIT License.