uniapp 图标角标
uniapp 图标角标实现方法
在 uniapp 中实现图标角标功能,可以通过以下几种方式实现:
使用 uni.setTabBarBadge API
适用于底部 tabBar 的角标设置,支持动态更新数字或红点。
uni.setTabBarBadge({
index: 0, // tabBar 的哪一项,从0开始
text: '1' // 角标内容,可以是数字或空字符串(显示红点)
})
自定义组件实现
在页面任意位置实现角标效果,可以使用 CSS 绝对定位和样式控制。
<view class="icon-container">
<image src="/static/icon.png"></image>
<view class="badge">5</view>
</view>
.icon-container {
position: relative;
width: 50px;
height: 50px;
}
.badge {
position: absolute;
top: -5px;
right: -5px;
min-width: 16px;
height: 16px;
line-height: 16px;
text-align: center;
border-radius: 8px;
background-color: #ff0000;
color: #ffffff;
font-size: 10px;
padding: 0 3px;
}
使用第三方 UI 库
如 uView、ColorUI 等组件库都提供了现成的角标组件。
<u-badge :count="3" :offset="[10, 10]">
<image src="/static/icon.png" style="width: 50px; height: 50px;"></image>
</u-badge>
注意事项
- 在 H5 端,tabBar 角标可能受浏览器限制
- 小程序端需要真机调试才能看到效果
- 角标位置和样式需要根据实际需求调整
- 动态更新角标内容时,注意数据绑定的使用
兼容性处理
不同平台对角标的支持程度不同,建议通过条件编译处理差异:
// #ifdef APP-PLUS || MP-WEIXIN
uni.setTabBarBadge({
index: 0,
text: '1'
})
// #endif
// #ifdef H5
// 使用自定义角标方案
// #endif






