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

如何实现带透明背景的动态边框Hover效果?

实现透明背景+保留动态旋转边框hover效果

要在保留现有动态hover边框效果的基础上实现背景透明,只需调整核心伪元素样式并确保文字层级正确,具体修改如下:

关键修改点

  • 将中间遮罩的黑色背景改为透明,让底层背景可见
  • 把旋转的白色填充块改为边框样式,避免遮挡透明背景
  • 提升文字层级,防止被伪元素覆盖

修改后的完整代码

CSS 代码

body {
  background-color: rgb(58, 58, 58);
}

.selects {
  float: right;
  margin-right: 10vh;
  padding: 0;
  margin-top: 10vh;
}

.selects li {
  display: inline-block;
  cursor: pointer;
  font-size: 19px;
  position: relative;
  z-index: 1; /* 确保文字在最上层 */
  color: white; /* 可选:给文字加颜色,方便在透明背景上看清 */
}

.animatedCard {
  place-items: center;
  display: inline-grid;
  position: relative;
  height: 50px;
  width: 230px;
  background: transparent;
  overflow: hidden;
  border-radius: 8px; /* 把圆角移到容器上,统一效果 */
}

.animatedCard::before {
  position: absolute;
  content: "";
  width: 150%;
  height: 150%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(45deg);
  background: transparent;
  border: 2px solid white; /* 把填充块改为白色边框 */
  box-sizing: border-box;
}

.animatedCard:hover::before {
  animation: animate 2s linear infinite;
}

@keyframes animate {
  from {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  to {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}

.animatedCard::after {
  position: absolute;
  content: "";
  inset: 2px;
  background: transparent; /* 将黑色背景改为透明 */
  border-radius: 6px; /* 对应容器的圆角,调整内边距后的圆角值 */
}

.selects li:hover {
  transform: scale(1.2);
  transition: 100ms;
}

HTML 代码

<ul class="selects">
  <div class="animatedCard">
    <li>¿Que es Front-end?</li>
  </div>
</ul>

效果说明

  • 鼠标hover时,白色边框会围绕卡片中心旋转,完整保留原动态效果
  • 卡片中间区域完全透明,可直接看到页面背景
  • 文字始终显示在最上层,不会被旋转的边框遮挡

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 20:39:52