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

如何修改CSS+JS代码实现图片悬停时粉色持续闪烁/发光效果

实现鼠标悬停时图片粉色持续闪烁/发光效果

解决方案步骤:

  1. 添加CSS闪烁动画:定义循环的发光关键帧动画,通过阴影的颜色、大小变化实现闪烁效果。
  2. JS事件监听:给图片容器绑定mouseenter和mouseleave事件,动态添加/移除动画类来控制动画启停。
  3. 兼容原有样式:确保新增动画与现有按钮样式不冲突,保持整体视觉协调。

修改后的完整代码:

CSS 部分

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
  outline: 0;
  border: none;
}
body {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #22232e;
}
button {
  display: flex;
  justify-content: center;
  align-items: center;
  background: transparent;
  position: relative;
}
button .btn-img{
  padding: 10px 15px;
  font-size: 13px;
  text-align: center;
  color: #e0ffff;
  border: 2px solid rgba(255,255,255,0.1);
  border-radius: 15px;
  background: rgba(0,73,88,0.05);
  backdrop-filter: blur(15px);
  cursor: pointer;
  z-index: 1;
  transition: 0.2s;
}
button::before{
  content: '';
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: -3px;
  width: 25%;
  height: 10px;
  background: #00c2cb;
  border-radius: 10px;
  transition: .5s;
  box-shadow: 0 0 10px rgba(0,194,203,0.5);
}
button:hover::before{
  bottom: 0;
  height: 40%;
  width: 90%;
  border-radius: 30px;
  transition-delay: 0.5s;
  background: #db0079;
}

/* 新增粉色闪烁动画 */
@keyframes pink-glow {
  0%, 100% {
    box-shadow: 0 0 5px #db0079, 0 0 10px #db0079, 0 0 15px #db0079;
  }
  50% {
    box-shadow: 0 0 10px #ff4da6, 0 0 20px #ff4da6, 0 0 30px #ff4da6;
  }
}

/* 动画触发类 */
.glow-active {
  animation: pink-glow 1s infinite alternate;
}

HTML 部分

<button>
  <span class="btn-img">
    <img class="glow-img" src="https://img1.daumcdn.net/thumb/R48x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FtKZbq%2FbtsEb7zLet0%2FHDOJKKBGmhp3kWrFZXXTFk%2Fimg.png"><br>
    <div id="tr" style="display: table-cell; text-align: center; color: rgb(255,255,255);">Sample</div>
  </span>
</button>

JavaScript 部分

const glowImg = document.querySelector('.glow-img');
const btnContainer = document.querySelector('.btn-img');

// 鼠标进入时启动闪烁动画
btnContainer.addEventListener('mouseenter', () => {
  glowImg.classList.add('glow-active');
});

// 鼠标离开时停止闪烁动画
btnContainer.addEventListener('mouseleave', () => {
  glowImg.classList.remove('glow-active');
});

效果说明:

  • 动画pink-glow通过交替切换阴影的深浅粉色和扩散范围,实现柔和的持续闪烁发光效果,infinite设置循环播放,alternate让动画反向过渡,避免生硬跳转。
  • JS监听按钮容器的鼠标事件,确保鼠标在整个按钮区域内都能触发动画,离开时立即停止。
  • 若需要让整个按钮区域(而非仅图片)闪烁,可将动画类绑定到.btn-img元素上,只需修改JS中添加类的目标元素即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 08:40:58