[英]My two CSS animations happen at the same time. How do make it so one only happens after the other is completely finished?
I have a sliding side menu bar. 我有一个滑动的侧边菜单栏。 I made two of them.
我做了两个。 One with a menu icon you can click to close it and open the other menu bar with a close icon.
一个带有菜单图标的您可以单击以将其关闭,然后打开另一个带有关闭图标的菜单栏。
The problem is my JS code and also the CSS transition animation don't give enough time for one to close before the other start to expand. 问题是我的JS代码以及CSS过渡动画没有足够的时间让一个关闭,而另一个则没有开始扩展。 So the two menu bars meet in the middle and create a messy animation.
因此,两个菜单栏在中间相遇并创建了混乱的动画。
function closeIt(){ document.getElementById('mysidenav').style.width='0px'; document.getElementById('mysidenav2').style.width='20%'; } function openIt(){ document.getElementById('mysidenav').style.width='20%'; document.getElementById('mysidenav2').style.width='0px'; }
*{ margin:0; padding:0; } html,body{ height:100%; width:100%; } .sidenav{ height:100%; width:20%; background:#111; transition:1s; overflow-x:hidden; } .sidenav a{ font-size:90px; color:#818181; } /*SECOND SIDE BAR*/ .sidenav2{ height:100%; width:0%; background:#111; overflow-x:hidden; position:fixed; top:0; transition:1s; } .sidenav2 a { font-size:50px; color:#818181; }
<div id='mysidenav'class='sidenav'> <a onclick='closeIt()'>×<a> </div> <div id='mysidenav2'class='sidenav2'> <a onclick='openIt()'>☰<a> </div>
You need to set a delay on the transition: 您需要为过渡设置延迟:
transition-delay:1s;
The problem is that this will apply when opening and when closing. 问题是,这将在打开和关闭时适用。 So to fix that, add and remove a CSS class instead of poking at the
width
directly: 因此,要解决此问题,请添加和删除CSS类,而不是直接在
width
上戳:
.hidden-sidenav {
width:0;
transition-delay:0s;
}
In the example below, I've also added transition-timing-function
s so the closing speeds up over time, and the opening slows down. 在下面的示例中,我还添加了
transition-timing-function
因此关闭随着时间的推移会加快,打开的速度会变慢。 And reduced the timings a bit. 并减少了时间。 Looks nicer that way.
这样看起来更好。
function closeIt(){ document.getElementById('mysidenav').classList.add('hidden-sidenav'); document.getElementById('mysidenav2').classList.remove('hidden-sidenav'); } function openIt(){ document.getElementById('mysidenav').classList.remove('hidden-sidenav'); document.getElementById('mysidenav2').classList.add('hidden-sidenav'); }
*{ margin:0; padding:0; } html,body{ height:100%; width:100%; } .sidenav{ height:100%; width:20%; background:#111; transition:0.4s; transition-delay:0.4s; transition-timing-function:ease-out; overflow-x:hidden; } .sidenav a{ font-size:90px; color:#818181; } /*SECOND SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIDDDDDDDDDDDDDDDDDDDDEEEEEEE BAR*/ .sidenav2{ height:100%; width:20%; /* Changed to 20%: visible by default. */ background:#111; overflow-x:hidden; position:fixed; top:0; transition:0.4s; transition-timing-function:ease-out; transition-delay:0.4s; } .sidenav2 a { font-size:50px; color:#818181; } .hidden-sidenav { /* Must come after .sidenav and .sidenav2 to override them. */ transition-delay:0s; transition-timing-function:ease-in; width:0; }
<div id='mysidenav'class='sidenav'> <a onclick='closeIt()'>×<a> </div> <div id='mysidenav2'class='sidenav2 hidden-sidenav'> <a onclick='openIt()'>☰<a> </div>
The most performant way to do this is with CSS. 最有效的方法是使用CSS。 Add a
transition-delay
. 添加
transition-delay
。
.sidenav2{
transition-delay: 1s;
}
function closeIt(){ document.getElementById('mysidenav').style.width='0px'; setTimeout(function () { document.getElementById('mysidenav2').style.width='20%'; }, 1000); } function openIt(){ document.getElementById('mysidenav2').style.width='0px'; setTimeout(function () { document.getElementById('mysidenav').style.width='20%'; }, 1000); }
*{ margin:0; padding:0; } html,body{ height:100%; width:100%; } .sidenav{ height:100%; width:20%; background:#111; transition:1s; overflow-x:hidden; } .sidenav a{ font-size:90px; color:#818181; } /*SECOND SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIDDDDDDDDDDDDDDDDDDDDEEEEEEE BAR*/ .sidenav2{ height:100%; width:0%; background:#111; overflow-x:hidden; position:fixed; top:0; transition:1s; } .sidenav2 a { font-size:50px; color:#818181; }
<div id='mysidenav'class='sidenav'> <a onclick='closeIt()'>×<a> </div> <div id='mysidenav2'class='sidenav2'> <a onclick='openIt()'>☰<a> </div>
Just add the setTimeout
that is equal to your transition time. 只需添加等于您的过渡时间的
setTimeout
即可。
You can do it with setTimeout()
, like this: 您可以使用
setTimeout()
,如下所示:
function closeIt() { document.getElementById('mysidenav').style.width = '0px'; window.setTimeout(function() { document.getElementById('mysidenav2').style.width = '20%'; }, 1000); } function openIt() { document.getElementById('mysidenav2').style.width = '0px'; window.setTimeout(function() { document.getElementById('mysidenav').style.width = '20%'; }, 1000); }
* { margin: 0; padding: 0; } html, body { height: 100%; width: 100%; } .sidenav { height: 100%; width: 20%; background: #111; transition: 1s; overflow-x: hidden; } .sidenav a { font-size: 90px; color: #818181; } /*SECOND SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIDDDDDDDDDDDDDDDDDDDDEEEEEEE BAR*/ .sidenav2 { height: 100%; width: 0%; background: #111; overflow-x: hidden; position: fixed; top: 0; transition: 1s; } .sidenav2 a { font-size: 50px; color: #818181; }
<div id='mysidenav' class='sidenav'> <a onclick='closeIt()'>×<a> </div> <div id='mysidenav2'class='sidenav2'> <a onclick='openIt()'>☰<a> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.