<template>
<view
v-if="show"
class="u-badge"
:class="[
isDot ? 'u-badge-dot' : '',
size == 'mini' ? 'u-badge-mini' : '',
type ? 'u-badge--bg--' + type : ''
]"
:style="[{
top: offset[0] + 'rpx',
right: offset[1] + 'rpx',
fontSize: fontSize + 'rpx',
position: absolute ? 'absolute' : 'static',
color: color,
backgroundColor: bgColor
}, boxStyle]"
>{{showText}}</view>
</template>
<script>
export default {
name: 'u-badge',
props: {
type: {
type: String,
default: 'error'
},
size: {
type: String,
default: 'default'
},
isDot: {
type: Boolean,
default: false
},
count: {
type: [Number, String]
},
overflowCount: {
type: Number,
default: 99
},
showZero: {
type: Boolean,
default: false
},
offset: {
type: Array,
default: () => {
return [20, 20]
}
},
absolute: {
type: Boolean,
default: true
},
fontSize: {
type: [String, Number],
default: '12'
},
color: {
type: String,
default: '#ffffff'
},
bgColor: {
type: String,
default: ''
},
isCenter: {
type: Boolean,
default: false
}
},
computed: {
boxStyle() {
let style = {}
if (this.isCenter) {
style.top = 0
style.right = 0
style.transform = 'translateY(-50%) translateX(50%)'
} else {
style.top = this.offset[0] + 'rpx'
style.right = this.offset[1] + 'rpx'
style.transform = 'translateY(0) translateX(0)'
}
if (this.size == 'mini') {
style.transform = style.transform + ' scale(0.8)'
}
return style
},
showText() {
if (this.isDot) return ''
else {
if (this.count > this.overflowCount) return `${this.overflowCount}+`
else return this.count
}
},
show() {
if (this.count == 0 && this.showZero == false) return false
else return true
}
}
}
</script>
<style lang="scss" scoped>
@import '../../libs/css/style.components.scss';
.u-badge {
display: inline-flex;
justify-content: center;
align-items: center;
line-height: 12rpx;
padding: 4rpx 8rpx;
border-radius: 50rpx;
font-size: rpx(20) !important;
&--bg--primary {
background-color: #2979ff;
}
&--bg--error {
background-color:#fa3534;
padding: 0 !important;
width: rpx(40) !important;
height: rpx(40) !important;
line-height:rpx(40) !important ;
top: rpx(-18) !important;
right: rpx(-18) !important;
font-size: rpx(20) !important;
text-align: center !important;
border-radius: rpx(20)!important;
}
&--bg--success {
background-color: #19be6b;
}
&--bg--info {
background-color: #909399;
}
&--bg--warning {
background-color: #ff9900;
}
}
.u-badge-dot {
height: 8rpx;
width: 8rpx;
border-radius: 50rpx;
line-height: 1;
}
.u-badge-mini {
transform: scale(0.8);
transform-origin: center center;
}
.u-info {
background: #909399;
color: #fff;
}
</style>