CSS parent div 上的过渡导致绝对 child div 跳转?
CSS transition on parent div causing absolute child divs to jump?
我正在制作一个圆形的汉堡包菜单按钮,它在悬停时进行动画处理,从中心开始生长,三栏 child divs 在 parent [=36= 的中心] 圆圈。
parent div 有固定的定位以启用 "grow" 效果。三个汉堡菜单行绝对定位 divs。
悬停时,通过过渡动画的所有内容都很好,除了三个 child div 在悬停时从 右下角 跳转和 左上角 鼠标移开。我试过调整边距、width/height 和定位,但我被卡住了。我错过了什么?
.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>
问题是因为您正在过渡容器的尺寸变化,但没有对条形图的位置变化应用等效过渡。因此,条形图会根据悬停时容器的原始大小跳转到它们的新位置(意思是,随着 top
和 left
值变大,它会移动到右下角)并跳转到它们在悬停时在放大容器中的原始位置(意思是,随着 top
和 left
变小,它会移动到左上角)。条形图最终会在过渡结束时移动到中心,因为容器从各个方向拉伸。
如果您向条形图添加等效的 300 毫秒 transition
,那么一切看起来都很好。
请注意,栏的左侧位置仍然有一个小跳跃,但这可以通过调整悬停时容器的 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;
}
<div class="circle-nav">
<div class="top-bar"></div>
<div class="mid-bar"></div>
<div class="bot-bar"></div>
</div>
我正在制作一个圆形的汉堡包菜单按钮,它在悬停时进行动画处理,从中心开始生长,三栏 child divs 在 parent [=36= 的中心] 圆圈。
parent div 有固定的定位以启用 "grow" 效果。三个汉堡菜单行绝对定位 divs。
悬停时,通过过渡动画的所有内容都很好,除了三个 child div 在悬停时从 右下角 跳转和 左上角 鼠标移开。我试过调整边距、width/height 和定位,但我被卡住了。我错过了什么?
.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>
问题是因为您正在过渡容器的尺寸变化,但没有对条形图的位置变化应用等效过渡。因此,条形图会根据悬停时容器的原始大小跳转到它们的新位置(意思是,随着 top
和 left
值变大,它会移动到右下角)并跳转到它们在悬停时在放大容器中的原始位置(意思是,随着 top
和 left
变小,它会移动到左上角)。条形图最终会在过渡结束时移动到中心,因为容器从各个方向拉伸。
如果您向条形图添加等效的 300 毫秒 transition
,那么一切看起来都很好。
请注意,栏的左侧位置仍然有一个小跳跃,但这可以通过调整悬停时容器的 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;
}
<div class="circle-nav">
<div class="top-bar"></div>
<div class="mid-bar"></div>
<div class="bot-bar"></div>
</div>