简体   繁体   English

如何在入口和出口处设置一个 div 动画?

[英]How to have a div animate on both entrance and exit?

I'm trying to make my div have two different transition animations I took from Animista: one of entry and one on exit.我试图让我的 div 有两种不同的过渡动画,我从 Animista 中获取:一种是进入,一种是退出。

To do that, I have a classlist toggle function that takes away or add an Open-Menu-Class and Close-Menu-Class.为此,我有一个类列表切换开关 function,它删除或添加一个 Open-Menu-Class 和 Close-Menu-Class。 Then, I set the relevant animations.然后,我设置了相关的动画。 But it seems like only the entry one is working, and the exit one has no animations at all.但似乎只有入口一个在工作,而出口一个根本没有动画。 Not very sure what is going on.不太确定发生了什么。

https://codepen.io/BenjaminTham67/pen/bGjEBXZ https://codepen.io/BenjaminTham67/pen/bGjEBXZ

animation when the menu is closed
.Hamburger-Menu-Closed {
    -webkit-animation: scale-out-hor-right 0.5s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
    animation: scale-out-hor-right 0.5s cubic-bezier(0.550, 0.085, 0.680, 0.530) both;
}

animation when the menu is open
.Hamburger-Menu-Opened {
    -webkit-animation: scale-in-hor-right 0.5s cubic-bezier(0.250, 0.460, 0.450, 0.940) both;
    animation: scale-in-hor-right 0.5s cubic-bezier(0.250, 0.460, 0.450, 0.940) both;
}

Note: somethings needed to be changed, so the buttons are just big red squares注意:有些东西需要改变,所以按钮只是红色的大方块

Apparently animations cannot be played from display:flex to display:none.显然动画不能从 display:flex 播放到 display:none。 That's why the exit animation don't work.这就是出口 animation 不起作用的原因。

You need to remove document.getElementById("hamburgerMenuList").style.display = "none";您需要删除document.getElementById("hamburgerMenuList").style.display = "none"; . . Your code will be work.您的代码将起作用。 Also you can use add and remove class instead of toggle .您也可以使用addremove class 而不是toggle

function openMobileMenu() {
    document.getElementById("hamburgerMenuList").style.display = "flex";
    var openMenu = document.getElementById("hamburgerMenuList");
    openMenu.classList.remove("Hamburger-Menu-Closed");
    openMenu.classList.add("Hamburger-Menu-Opened");

}

function closeMobileMenu() {

    var closeMenu = document.getElementById("hamburgerMenuList");
    closeMenu.classList.remove("Hamburger-Menu-Opened");
    closeMenu.classList.add("Hamburger-Menu-Closed");
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM