纯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"/>
相关推荐
Patrick_Wilson13 小时前
router.replace 之后紧跟 reload,页面为什么无限刷新?
javascript·react.js·浏览器
小小小小宇13 小时前
OpenMemory MCP
前端
和平宇宙13 小时前
AI笔记005. hermes-DeepSeek V4 Pro, 128K上下文引发的探索
前端·人工智能·笔记
IT_陈寒14 小时前
Redis持久化这个坑,我爬了一整天才出来
前端·人工智能·后端
naildingding14 小时前
3-ts接口 Interface
前端·typescript
mONESY14 小时前
JavaScript 栈、队列、数组与链表核心知识点总结
javascript·面试
小小前端仔LC14 小时前
Node.js + LangChain + React:搭建个人知识库(六)- “吃什么”项目实战:从700+菜谱入库到Taro H5端JSON渲染
前端·后端
ZengLiangYi14 小时前
TypeScript 项目配置:tsconfig、ESM、路径别名
javascript·typescript·aigc
晓131315 小时前
【Cocos Creator 3.x】篇——第二章 入门
前端·javascript·游戏引擎
想要成为糕糕手15 小时前
前端必修课:JavaScript 数组与数据结构底层逻辑全解析
javascript·数据结构·面试