简体   繁体   English

如何在 for 循环中对菜单中的每个图标应用淡出 animation

[英]How would I apply a fade out animation to each of the icons in my menu within a for loop

im currently working on an animation where there's a delay between each of the icons when fading out.我目前正在研究 animation,其中每个图标之间在淡出时都有延迟。 The fade out animation works fine, but only on the first icon and im unsure on why this is.淡出 animation 工作正常,但仅在第一个图标上,我不确定这是为什么。 Could some one maybe point out the mistake im making?有人可以指出我犯的错误吗?

Javascript code: Javascript 代码:

function closeHorizontalWindow()
{
const delay = 150;

const reasons = ['btnReady', 'btnNotReady', 'btnBreak', 'btnEmail', 'btnLunch', 
'btnComfortBreak', 'btnMeeting', 'btnChat']

for (let i = 0; i < reasons.length; i++) {   
    document.getElementById(reasons[i]).style.animation = `reasonsfadeout 0.45s ease-out 
forwards ${delay}ms` 
    delay+=150       
}

CSS code: CSS 代码:

@keyframes reasonsfadeout
{
  from{opacity: 1; }
  to {opacity: 0; } 
}

} }

Thankyou谢谢

You can use an interval to iterate instead of a regular for-loop.您可以使用间隔来迭代而不是常规的 for 循环。

function closeHorizontalWindow()
{
    const delay = 150;
    const reasons = ['btnReady', 'btnNotReady', 'btnBreak', 'btnEmail', 'btnLunch', 
'btnComfortBreak', 'btnMeeting', 'btnChat']

    let i = 0;
    const interval = setInterval(function(){
        document.getElementById(reasons[i]).style.animation = `reasonsfadeout 0.45s ease-out forwards ${delay}ms`;
        i++;
        if(i>=reasons.length){
            clearInterval(interval);
        }
    }, delay);
}


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

相关问题 我如何使用淡入/淡入淡出/或任何动画来平滑显示我的 HTML、CSS 和 JS 导航菜单? - How do I use fade-in/ease-in-out/or any animation to smooth out the revealing of my HTML, CSS and JS navigation menu? 如何淡出div,更改背景图像并在循环中淡入? - How to fade div out, change background image and fade in within a loop? 如何将淡出动画添加到可点击的下拉菜单中 - How to add fade out animation to the clickable dropdown menu 单击菜单时,如何根据显示的菜单淡入和淡出两个无序列表? - How do I make two unordered list fade in and fade out when my menu is clicked depending on which is showing? 如何在循环中淡入和淡出文本 - How to fade in and fade out text in loop 如何淡入/淡出带有子菜单的 jQuery 菜单? - How do I fade in/out a jQuery menu with sub menus? 全屏菜单覆盖:如何使其淡入淡出? - Fullscreen menu overlay: how do I make it fade in and out? 使用淡入和/或淡出添加我的2张图像的动画 - Add animation of my 2 images using fade in and/or fade out jQuery Animation 淡入淡出……如何创建第二个事件? - jQuery Animation fade in, fade out…How do I create the second event? 我将如何做一个 for 循环,等待我的函数为每个循环完成? - How would I do a for loop that waits until my function has completed for each loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM