Badge 角标

基本用法

部分组件可以设置 badge 属性来显示角标。

1.2.2 及之前版本只支持头像组件

1.2.3 开始支持按钮、链接、模板组件。

```schema: scope=”body” [ { “type”: “avatar”, “badge”: { “mode”: “text”, “text”: 10 } }, { “type”: “divider” }, { “type”: “action”, “label”: “按钮”, “badge”: { “mode”: “text”, “text”: 15 } }, { “type”: “divider” }, { “type”: “link”, “href”: “https://www.baidu.com“, “body”: “百度一下,你就知道”, “badge”: { “position”: “top-right” } }, { “type”: “divider” }, { “type”: “tpl”, “tpl”: “Hello ${text}”, “badge”: { “mode”: “text”, “text”: 25 } }, { “type”: “divider” }, { “type”: “icon”, “icon”: “cloud”, “className”: “text-info text-xl”, “badge”: { “position”: “top-left” } }, { “type”: “divider” }, { “type”: “action”, “label”: “按钮”, “badge”: { “mode”: “ribbon”, “text”: “HOT” } }, ]

  1. ## 显示文字或数值
  2. 设置 `mode` `text`,通过 `text` 属性来显示文字或数字,数值为零的时候将不显示。
  3. ```schema: scope="body"
  4. [
  5. {
  6. "type": "avatar",
  7. "badge": {
  8. "mode": "text",
  9. "text": 10
  10. }
  11. },
  12. {
  13. "type": "avatar",
  14. "className": "m-l",
  15. "badge": {
  16. "mode": "text",
  17. "text": 0
  18. }
  19. },
  20. {
  21. "type": "avatar",
  22. "className": "m-l",
  23. "badge": {
  24. "mode": "text",
  25. "text": "new"
  26. }
  27. }
  28. ]

显示位置

通过 position 来控制角标显示位置,默认 top-right,还可以设置为 top-leftbottom-leftbottom-right

```schema: scope=”body” [ { “type”: “avatar”, “badge”: { “position”: “top-left” } }, { “type”: “avatar”, “className”: “m-l”, “badge”: { “position”: “bottom-left” } }, { “type”: “avatar”, “className”: “m-l”, “badge”: { “position”: “bottom-right” } } ]

  1. 通过 `offset` 来控制角标显示位置,offset优先级大于position。当设置了offset后,以postiontop-right为基准进行定位。offsetmode=ribbon下设置无效。
  2. ```schema: scope="body"
  3. [
  4. {
  5. "type": "avatar",
  6. "badge": {
  7. "offset": [10, 10]
  8. }
  9. }
  10. ]

动态数字

text 可以取上下文变量。

  1. {
  2. "data": {
  3. "myData": 10
  4. },
  5. "type": "page",
  6. "body": [
  7. {
  8. "type": "avatar",
  9. "badge": {
  10. "mode": "text",
  11. "visibleOn": "this.myData > 1",
  12. "text": "${myData}"
  13. }
  14. }
  15. ]
  16. }

设置封顶值

通过 overflowCount 可以设置封顶值。超过 overflowCount 的会显示为 ${overflowCount}+,默认的 overflowCount 为 99

  1. {
  2. "type": "page",
  3. "body": [
  4. {
  5. "type": "avatar",
  6. "badge": {
  7. "mode": "text",
  8. "text": 10,
  9. "overflowCount": 9
  10. }
  11. }
  12. ]
  13. }

动态控制是否显示角标

角标可以直接写表达式来判断是否显示

  1. {
  2. "data": {
  3. "myData": 10
  4. },
  5. "type": "page",
  6. "body": [
  7. {
  8. "type": "avatar",
  9. "badge": "this.myData > 1"
  10. },
  11. {
  12. "type": "avatar",
  13. "className": "m-l",
  14. "badge": "this.myData > 10"
  15. }
  16. ]
  17. }

还可以通过 visibleOn 表达式来动态控制,这样还能设置其他属性

  1. {
  2. "data": {
  3. "myData": 10
  4. },
  5. "type": "page",
  6. "body": [
  7. {
  8. "type": "avatar",
  9. "badge": {
  10. "visibleOn": "this.myData > 1"
  11. }
  12. },
  13. {
  14. "type": "avatar",
  15. "className": "m-l",
  16. "badge": {
  17. "visibleOn": "this.myData > 10"
  18. }
  19. }
  20. ]
  21. }

设置大小

通过 size 来控制大小

```schema: scope=”body” [ { “type”: “avatar”, “badge”: { “mode”: “text”, “text”: 10, “size”: 20 } }, { “type”: “avatar”, “className”: “m-l”, “badge”: { “mode”: “text”, “text”: 10, “size”: 12 } }, { “type”: “avatar”, “className”: “m-l”, “badge”: { “size”: 12 } }, { “type”: “avatar”, “className”: “m-l”, “badge”: { “size”: 4 } } ]

  1. ## 设置角标级别
  2. 通过 `level` 来设置角标级别,改变角标背景颜色
  3. ```schema: scope="body"
  4. [
  5. {
  6. "type": "avatar",
  7. "badge": {
  8. "mode": "text",
  9. "text": 10,
  10. "size": 20,
  11. "level": "success"
  12. }
  13. },
  14. ]

是否显示动画

在默认点状态下,可以通过设置 animation 属性来控制是否显示动画

```schema: scope=”body” [ { “type”: “avatar”, “badge”: { “animation”: true } } ]

  1. ## 自定义样式
  2. 通过 `style` 来控制展现样式,比如背景及文字的颜色
  3. ```schema: scope="body"
  4. [
  5. {
  6. "type": "avatar",
  7. "badge": {
  8. "mode": "text",
  9. "text": 10,
  10. "style": {
  11. "background": "#46C93A"
  12. }
  13. }
  14. },
  15. {
  16. "type": "avatar",
  17. "className": "m-l",
  18. "badge": {
  19. "mode": "text",
  20. "text": 10,
  21. "style": {
  22. "background": "#1A5CFF"
  23. }
  24. }
  25. }
  26. ]

属性表

属性名 类型 默认值 说明
mode string dot 角标类型,可以是 dot/text/ribbon
text textnumber 角标文案,支持字符串和数字,在mode=’dot’下设置无效
size number 角标大小
level string 角标级别, 可以是info/success/warning/danger, 设置之后角标背景颜色不同
overflowCount number 99 设置封顶的数字值
position string top-right 角标位置, 可以是top-right/top-left/bottom-right/bottom-left
offset number[top, left] 角标位置,优先级大于position,当设置了offset后,以postion为top-right为基准进行定位
className string 外层 dom 的类名
animation boolean 角标是否显示动画
style object 角标的自定义样式
visibleOn 表达式 控制角标的显示隐藏