纯CSS实现未读消息显示99+

在大佬那看到这个小技巧,我觉得这个功能点还挺常用,所以给大家分享下具体的实现。当未读消息数小于100的时候显示准确数值,大于99的时候显示99+。

1. 实现效果
2. 组件封装
html 复制代码
<template>
  <span class="col">
    <sup :style="{'--num':num}">{{num}}</sup>
  </span>
</template>

<script>
export default {
  name: 'unread',
  props:{
    num:{
      type: Number,
      default: 0
    }
  }
}
</script>
<style>
.col {
  display: inline-flex;
  width: 4rem; height: 4rem;
  align-items: center;
  justify-content: center;
}

.col sup {
  position: absolute;
  box-sizing: border-box;
  min-width: 1rem;
  padding: 0 0.1875rem;
  color: #fff;
  font-size: min(.75rem, calc(10000px - var(--num) * 100px));
  line-height: 1.2;
  text-align: center;
  background-color: #eb4646;
  border: 1px solid #fff;
  border-radius: 1rem;
  transform: translate(calc(40% + .375rem), -.75rem);
  /* 数值为0的时候隐藏 */
  opacity: var(--num);
}
.col sup::before {
  content: '99+';
  font-size: min(.75rem, calc(var(--num) * 100px - 9900px));
}
</style>
3. 使用
html 复制代码
      <Unread :num="0"/> // 为0隐藏
      <Unread :num="9"/>
      <Unread :num="99"/>
      <Unread :num="999"/>
相关推荐
LFly_ice9 分钟前
学习React-22-Zustand
前端·学习·react.js
南山安24 分钟前
从反转字符串看透面试官的“内心戏”:你的算法思维到底怎么样?
javascript·算法·面试
东华帝君25 分钟前
vue3自定义v-model
前端
www_stdio25 分钟前
用 localStorage 打造本地待办清单:一个轻量级的前端实践
javascript·css·json
fruge44 分钟前
搭建个人博客 / 简历网站:从设计到部署的全流程(含响应式适配)
前端
光影少年1 小时前
css影响性能及优化方案都有哪些
前端·css
呆呆敲代码的小Y1 小时前
2025年多家海外代理IP实战案例横向测评,挑选适合自己的
前端·产品
q***3751 小时前
爬虫学习 01 Web Scraper的使用
前端·爬虫·学习
v***5651 小时前
Spring Cloud Gateway
android·前端·后端
b***59431 小时前
分布式WEB应用中会话管理的变迁之路
前端·分布式