You need to enable JavaScript to run this app. Shopify站点实现CSS transition后按钮悬停背景透明效果求助
优惠活动
大模型
产品
解决方案
定价
更多

Shopify站点实现CSS transition后按钮悬停背景透明效果求助

实现方案

直接替换你现有CSS代码为下方内容即可实现需求:

/* 按钮基础样式补充,确保伪元素定位正常 */
.btn.btn1 {
  position: relative;
  overflow: hidden;
}

.btn.btn1::before {
  top: 0px;
  bottom: 0px;
  right: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
  position: absolute;
  z-index: -1;
  content: '';
  transform: scaleX(0);
  transform-origin: left;
  /* 多属性分层过渡:transform先执行600ms,背景色延迟600ms再执行过渡 */
  transition: transform 600ms ease-in-out, background 300ms ease-in-out 600ms;
  background: #E9E9E9;
}

.btn.btn1:hover::before,
.btn.btn1:focus::before {
  transform: scaleX(1);
  background: transparent;
  /* hover状态下优先级调整:背景色立刻恢复灰色,优先执行滑入动画 */
  transition: transform 600ms ease-in-out, background 0s ease 0s;
}

.btn.btn1:hover,
.btn.btn1:focus {
  color: gray !important;
  z-index: 1;
}

调整说明

  • 补充了按钮本身的position: relative属性,这是绝对定位伪元素能相对按钮生效的前提,遗漏会导致动画位置错乱
  • 通过分层过渡规则实现你要的效果:hover后先执行600ms的灰色背景左滑入场,入场结束后自动触发背景从灰色变透明的过渡,背景透明的过渡时长可自行修改background 300ms中的300参数调整快慢
  • 区分了hover和非hover状态的过渡逻辑,避免鼠标移开时动画顺序错乱,移开时会先恢复灰色背景再执行滑出收起动画,视觉效果更流畅

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.17 08:15:01