父 div 上的 CSS 转换导致绝对子 div 跳转?

Ce.*_*Ce. 5 css css-transitions css-animations

我正在开发一个圆形汉堡菜单按钮,该按钮在悬停时具有动画效果,从中心开始生长,三个栏子 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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="circle-nav">
  <div class="top-bar"></div>
  <div class="mid-bar"></div>
  <div class="bot-bar"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

Har*_*rry 3

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

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

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

.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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="circle-nav">
  <div class="top-bar"></div>
  <div class="mid-bar"></div>
  <div class="bot-bar"></div>
</div>
Run Code Online (Sandbox Code Playgroud)