Skip to content

LimeBadge 徽标

  • 在右上角展示徽标数字或小红点。

安装

插件市场入口 导入即可,首次导入可能需要重新编译

代码演示

基础用法

设置 content 属性后,Badge 会在子元素的右上角显示对应的徽标,也可以通过 dot 来显示小红点。

html
<l-badge :content="5">
  <view class="child" />
</l-badge>
<l-badge :content="10">
  <view class="child" />
</l-badge>
<l-badge content="Hot">
  <view class="child" />
</l-badge>
<l-badge dot>
  <view class="child" />
</l-badge>

<style>
  .child {
    width: 40px;
    height: 40px;
    background: #f2f3f5;
    border-radius: 4px;
  }
</style>

最大值

设置 max 属性后,当 content 的数值超过最大值时,会自动显示为 {max}+

html
<l-badge :content="20" max="9">
  <view class="child" />
</l-badge>
<l-badge :content="50" max="20">
  <view class="child" />
</l-badge>
<l-badge :content="200" max="99">
  <view class="child" />
</l-badge>

自定义颜色

通过 color 属性来设置徽标的颜色。

html
<l-badge :content="5" color="#1989fa">
  <view class="child" />
</l-badge>
<l-badge :content="10" color="#1989fa">
  <view class="child" />
</l-badge>
<l-badge dot color="#1989fa">
  <view class="child" />
</l-badge>

自定义徽标内容

通过 content 插槽可以自定义徽标的内容,比如插入一个图标。

html
<l-badge>
  <view class="child" />
  <template #content>
    你好
  </template>
</l-badge>
css
.badge-icon {
  display: block;
  font-size: 10px;
  line-height: 16px;
}

自定义徽标位置

通过 position 属性来设置徽标的位置。

html
<l-badge :content="10" position="top-left">
  <view class="child" />
</l-badge>
<l-badge :content="10" position="bottom-left">
  <view class="child" />
</l-badge>
<l-badge :content="10" position="bottom-right">
  <view class="child" />
</l-badge>

独立展示

当 Badge 没有子元素时,会作为一个独立的元素进行展示。

html
<l-badge :content="20" />

<l-badge :content="200" max="99" />

查看示例

  • 导入后直接使用这个标签查看演示效果
html
<!-- // 代码位于 uni_modules/lime-badge/compoents/lime-badge -->
<lime-badge />

插件标签

  • 默认 l-badge 为 component
  • 默认 lime-badge 为 demo

关于vue2的使用方式

  • 插件使用了composition-api, 如果你希望在vue2中使用请按官方的教程vue-composition-api配置
  • 关键代码是: 在main.js中 在vue2部分加上这一段即可
js
// vue2
import Vue from 'vue'
import VueCompositionAPI from '@vue/composition-api'
Vue.use(VueCompositionAPI)

API

Props

参数说明类型默认值
content徽标内容number | string-
color徽标背景颜色string#ee0a24
dot是否展示为小红点booleanfalse
max最大值,超过最大值会显示 {max}+,仅当 content 为数字时有效number | string-
offset设置徽标的偏移量,数组的两项分别对应水平向右和垂直向下方向的偏移量,默认单位为 px[number | string, number | string]-
show-zero当 content 为数字 0 或字符串 '0' 时,是否展示徽标booleantrue
position徽标位置,可选值为 top-left bottom-left bottom-rightstringtop-right

Slots

名称说明
default徽标包裹的子元素
content自定义徽标内容

主题定制

样式变量

组件提供了下列 CSS 变量,可用于自定义样式。

名称默认值描述
--l-badge-size16px-
--l-badge-colorwhite-
--l-badge-padding0 3px-
--l-badge-font-size12px-
--l-badge-font-weightbold)-
--l-badge-border-width1px-
--l-badge-background#F53F3F-
--l-badge-dot-color#F53F3F-
--l-badge-dot-size8px-
--l-badge-font-apple-system-font, Helvetica Neue, Arial, sans-serif-
--l-badge-border-radius999px-

打赏

如果你觉得本插件,解决了你的问题,赠人玫瑰,手留余香。

源代码

组件源码