简体   繁体   English

我的两个CSS动画同时发生。 如何使一个只有在另一个完全完成之后才发生?

[英]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()'>&times<a> </div> <div id='mysidenav2'class='sidenav2'> <a onclick='openIt()'>&#9776<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()'>&times<a> </div> <div id='mysidenav2'class='sidenav2 hidden-sidenav'> <a onclick='openIt()'>&#9776<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()'>&times<a> </div> <div id='mysidenav2'class='sidenav2'> <a onclick='openIt()'>&#9776<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()'>&times<a> </div> <div id='mysidenav2'class='sidenav2'> <a onclick='openIt()'>&#9776<a> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次动画后完全删除元素。 - 仅使用 CSS - Remove the element completely after animation, each time. - only using CSS Javascript:如何使function2仅在function1完全完成之后执行? - Javascript: How do you make function2 execute only after function1 is completely finished? Highcharts NG一次只显示一个点。 动画制作 - Highcharts NG show only one point at a time. Animations HTML Canvas - 使动画一次发生一次,而不是同时发生多次 - HTML Canvas - Make animations happen one at a time, rather than multiple at the same time 多个用户同时使用一个电子表格,但一次只能使用一个。 该怎么办? - Multiple users using a spreadsheet at the same time, but it can only be used one at a time. What to do? 我如何使三个圆圈交替出现而又不能同时进行。 不要使用CSS - How would I make the 3 circles alternate without them going on at the same time. DON'T USE CSS 让两个精灵同时出现并移动。 一个使用 WASD 键,另一个使用箭头键。 (Javascript HTML) - Have two sprites appear and move at the same time. One using WASD keys and the other arrow keys. (Javascript HTML) 如何制作两个 div,只显示一个,并且可以实时交换? - How can I make two divs, so that only one will be displayed, and they can be swapped in real time? 如何使 animation 同时发生? - how to the make animation happen at the same time? 如何在五张不同的图像上设置相同的动画,但一次只能一张。 当一个动画时,另一个应该恢复其原始位置 - How to animate same on five different images but one at a time. When one is animated other should come in its original position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM