You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose带空白边框的用户在线指示器实现咨询

实现带空白边框的用户在线指示器

不用Canvas,纯CSS就能轻松实现这个效果,核心思路是利用绝对定位放置指示器,通过box-shadow模拟头像与指示器之间的空白边框,具体实现步骤如下:

1. HTML结构

用一个容器包裹头像图片和指示器元素:

<div class="user-avatar">
  <img src="你的头像路径" alt="用户头像">
  <span class="online-indicator"></span>
</div>

2. CSS样式

/* 头像容器:相对定位,用于定位指示器 */
.user-avatar {
  position: relative;
  width: 80px; /* 可根据需求调整尺寸 */
  height: 80px;
}

/* 头像样式:设置圆角实现圆形头像,方形头像可去掉border-radius */
.user-avatar img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  border: 2px solid #ffffff; /* 可选:头像本身的外边框 */
  object-fit: cover; /* 确保图片填充容器不变形 */
}

/* 在线指示器:绝对定位到头像右下角,用box-shadow做空白边框 */
.online-indicator {
  position: absolute;
  bottom: 4px;
  right: 4px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background-color: #2ecc71; /* 在线状态的绿色 */
  /* 关键:白色阴影模拟空白边框,3px是空白边框的宽度 */
  box-shadow: 0 0 0 3px #ffffff;
  /* 可选:如果需要给指示器再加一层贴合头像的边框,可追加阴影 */
  /* box-shadow: 0 0 0 3px #ffffff, 0 0 0 4px #e0e0e0; */
}

扩展状态

如果需要实现不同的用户状态,只需要修改指示器的背景色即可:

  • 离线状态:background-color: #95a5a6
  • 忙碌状态:background-color: #f39c12
  • 离开状态:background-color: #f1c40f

内容的提问来源于stack exchange,提问作者Ilhom Soliev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 19:08:20