【问题标题】:CSS transition on parent div causing absolute child divs to jump?父 div 上的 CSS 过渡导致绝对子 div 跳跃?
【发布时间】:2023-04-10 03:55:02
【问题描述】:

我正在开发一个圆形汉堡菜单按钮,该按钮在悬停时动画,从中心开始增长,三个条形子 div 居中在父 div 圈内。

父 div 具有固定的定位以启用“增长”效果。三个汉堡菜单行是绝对定位的 div。

当悬停时,通过过渡动画的一切都很好,除了三个子 div 在悬停时从 右下角 和鼠标移出时从 左上角 跳。我试过调整边距、宽度/高度和定位,但我被卡住了。我错过了什么?

.circle-nav {
  display: block;
  position: fixed;
  width: 44px;
  height: 44px;
  top: 35px;
  left: 35px;
  color: rgba(255, 255, 255, 0.8);
  background-color: rgb(136, 35, 24);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3), 0 2px 2px rgba(0, 0, 0, 0.05);
  -webkit-border-radius: 22px;
  -moz-border-radius: 22px;
  border-radius: 22px;
  transition: width 300ms ease, height 300ms ease, transform 300ms ease, background 300ms ease, border-radius 300ms ease, box-shadow 300ms ease, top 300ms ease, left 300ms ease;
}
.circle-nav:hover {
  width: 66px;
  height: 66px;
  top: 25px;
  left: 25px;
  background-color: rgb(187, 53, 39);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.3), 0 8px 6px rgba(0, 0, 0, 0.05);
  -webkit-border-radius: 33px;
  -moz-border-radius: 33px;
  border-radius: 33px;
}
.top-bar,
.mid-bar,
.bot-bar {
  width: 22px;
  height: 2px;
  position: absolute;
  left: 11px;
  background-color: #fff;
}
.top-bar {
  top: 14px;
}
.mid-bar {
  top: 21px;
}
.bot-bar {
  top: 28px;
}
.circle-nav:hover .top-bar {
  top: 24px;
}
.circle-nav:hover .mid-bar {
  top: 31px;
}
.circle-nav:hover .bot-bar {
  top: 38px;
}
.circle-nav:hover .top-bar,
.circle-nav:hover .mid-bar,
.circle-nav:hover .bot-bar {
  left: 22px;
}
<div class="circle-nav">
  <div class="top-bar"></div>
  <div class="mid-bar"></div>
  <div class="bot-bar"></div>
</div>

【问题讨论】:

    标签: css css-transitions css-animations


    【解决方案1】:

    问题是因为您正在转换容器的尺寸变化,但没有将等效转换应用于条的位置变化。正因为如此,在悬停时,条会根据容器的原始大小跳到新位置(意思是,当topleft 值更大时,它会移动到右下角)并跳到它们的原始位置悬停时放大的容器(意思是,随着topleft 变小,它会移动到左上角)。由于容器从各个方向拉伸,这些条最终会在过渡结束时移动到中心。

    如果您将等效的 300 毫秒 transition 添加到条形图上,那么一切看起来都很好。

    请注意,栏的左侧位置仍有一个小跳跃,但这可以通过调整悬停时容器的 left 位置来解决(就像我在 sn-p 中所做的那样)。

    .circle-nav {
      display: block;
      position: fixed;
      width: 44px;
      height: 44px;
      top: 35px;
      left: 35px;
      color: rgba(255, 255, 255, 0.8);
      background-color: rgb(136, 35, 24);
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3), 0 2px 2px rgba(0, 0, 0, 0.05);
      -webkit-border-radius: 22px;
      -moz-border-radius: 22px;
      border-radius: 22px;
      transition: width 300ms ease, height 300ms ease, transform 300ms ease, background 300ms ease, border-radius 300ms ease, box-shadow 300ms ease, top 300ms ease, left 300ms ease;
    }
    .circle-nav:hover {
      width: 66px;
      height: 66px;
      top: 25px;
      left: 24px; /* changed this to prevent the small adjustment during hover */
      background-color: rgb(187, 53, 39);
      box-shadow: 0 10px 24px rgba(0, 0, 0, 0.3), 0 8px 6px rgba(0, 0, 0, 0.05);
      -webkit-border-radius: 33px;
      -moz-border-radius: 33px;
      border-radius: 33px;
    }
    .top-bar,
    .mid-bar,
    .bot-bar {
      width: 22px;
      height: 2px;
      position: absolute;
      left: 11px;
      background-color: #fff;
      transition: all 300ms ease; /* added this to prevent the jump */
    }
    .top-bar {
      top: 14px;
    }
    .mid-bar {
      top: 21px;
    }
    .bot-bar {
      top: 28px;
    }
    .circle-nav:hover .top-bar {
      top: 24px;
    }
    .circle-nav:hover .mid-bar {
      top: 31px;
    }
    .circle-nav:hover .bot-bar {
      top: 38px;
    }
    .circle-nav:hover .top-bar,
    .circle-nav:hover .mid-bar,
    .circle-nav:hover .bot-bar {
      left: 22px;
    }
    <div class="circle-nav">
      <div class="top-bar"></div>
      <div class="mid-bar"></div>
      <div class="bot-bar"></div>
    </div>

    【讨论】:

    • 感谢哈利。我选择不使用此选项,而目前正在使用规模。但我很欣赏这个修复。由于我的菜单是一个复选框,我不确定这是否会因为其他几个变量而起作用。稍后我将对此进行更多研究。
    • @Ce.:该解决方案也应与复选框一起使用,但在我看来,缩放转换绝对是更好的选择。我假设您出于某种原因不想使用它。
    猜你喜欢
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 2015-06-03
    相关资源
    最近更新 更多