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

Styled Components按钮实现指定发光效果问题求助

解决内部发光效果问题

当前代码实现的是外部发光效果,要匹配示例中的内部发光,需做以下调整:

  • 给box-shadow添加inset关键字,将外部阴影转为内部阴影
  • 移除动画中对background-color的修改,保留原本的渐变背景,仅让发光效果做动画变化
  • 调整阴影的扩散范围与透明度,让发光效果更贴合示例的柔和感

修改后的代码:

const LiveNow = styled.span`
  display: inline-block;
  padding: 0.5rem 0.8rem;
  background: transparent linear-gradient(180deg, #eb1a30 0%, #ea1973 100%) 0% 0% no-repeat padding-box;
  border-radius: 8px;
  font-size: 13px;
  color: #fff;
  flex-grow: 0;
  z-index: 1;
  position: absolute;
  top: 16px;
  left: 16px;
  font-weight: bold;
  animation: glowing 1300ms infinite;
  -webkit-animation: glowing 1300ms infinite;
  -moz-animation: glowing 1300ms infinite;
  -o-animation: glowing 1300ms infinite;

  @-webkit-keyframes glowing {
    0% {
      -webkit-box-shadow: inset 0 0 3px rgba(235, 26, 48, 0.8);
    }
    50% {
      -webkit-box-shadow: inset 0 0 15px rgba(234, 25, 115, 0.8);
    }
    100% {
      -webkit-box-shadow: inset 0 0 3px rgba(235, 26, 48, 0.8);
    }
  }

  @keyframes glowing {
    0% {
      box-shadow: inset 0 0 3px rgba(235, 26, 48, 0.8);
    }
    50% {
      box-shadow: inset 0 0 15px rgba(234, 25, 115, 0.8);
    }
    100% {
      box-shadow: inset 0 0 3px rgba(235, 26, 48, 0.8);
    }
  }
`;

关键说明

  1. inset是实现内部发光的核心,它会把阴影绘制在元素内部区域
  2. 使用rgba颜色值调整发光透明度,避免效果过于生硬
  3. 保留原渐变背景,避免动画修改背景色导致渐变被覆盖

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 16:25:23