简体   繁体   English

将元素设置为可见不运行 css animation

[英]Setting element to visible does not run css animation

I have the following html code我有以下 html 代码

 var feedback = document.getElementById('openNotification'); feedback.addEventListener('click', function (){ a = document.getElementById("notification"); a.style.visibility = "visible"; });
 #notification { position: fixed; z-index: 101; bottom: 0; transform: translateY(calc(100% + 10px)); left: 10vw; right: 10vw; text-align: center; height: 20vh; overflow: hidden; background-color: #ededed; color: #000; } @keyframes slideUp { 0% { transform: translateY(calc(100% + 10px)); } 100% { transform: translateY(0); } } #notification { animation: slideUp 2.5s ease forwards; }
 <button id="openNotification"> Open notification </button> <:--If I remove the (style="visibility; hidden:") the animation works as expected--> <div id="notification" style="visibility; hidden;"> 121 </div>

The CSS for this div contains "transform" code to make the notification slide up the screen...此 div 的 CSS 包含“转换”代码以使通知在屏幕上向上滑动...

When I run the following code in a setTimeout function the notification simply appears on the screen and does not slide up as it should.当我在 setTimeout function 中运行以下代码时,通知只会出现在屏幕上,并且不会按应有的方式向上滑动。

 a = document.getElementById("notification"); a.style.visibility = "visible";

How do I fix this?我该如何解决?

On further testing I can see that the animation code seems to be running from the moment the code is loaded.在进一步的测试中,我可以看到 animation 代码似乎从代码加载的那一刻起就在运行。 I assume I need to somehow change this behaviour so the animation code is kicked off by the setTimout function or in this case the button click.我假设我需要以某种方式更改此行为,因此 animation 代码由 setTimout function 或在这种情况下单击按钮启动。 Any examples on how to do this?有关如何执行此操作的任何示例?

The animation takes place but as it only lasts a short time, by the time you come to push the button it has finished, and you then make the thing visible. animation 发生,但由于它只持续很短的时间,当你按下按钮时它已经完成,然后你就可以看到它。

Instead we remove the animation from the initial state of the element and add it (by adding a class in this case) only when you click the button.相反,我们仅在单击按钮时从元素的初始 state 中删除 animation 并添加它(在本例中通过添加 class)。

Note: if you want this to be repeatable you will have to include sensing the animationend event and removing the slide class at that point.注意:如果您希望这是可重复的,则必须包括感测 animationend 事件并在该点移除幻灯片 class。 Otherwise the system will think it's done the animation and needn't do it again.否则系统会认为它已经完成了 animation 并且不需要再做。

 var feedback = document.getElementById('openNotification'); feedback.addEventListener('click', function (){ a = document.getElementById("notification"); a.style.visibility = "visible"; a.classList.add('slide'); });
 #notification { position: fixed; z-index: 101; bottom: 0; transform: translateY(calc(100% + 10px)); left: 10vw; right: 10vw; text-align: center; height: 20vh; overflow: hidden; background-color: #ededed; color: #000; } @keyframes slideUp { 0% { transform: translateY(calc(100% + 10px)); } 100% { transform: translateY(0); } } #notification.slide { animation: slideUp 2.5s ease forwards; }
 <button id="openNotification"> Open notification </button> <:--If I remove the (style="visibility; hidden:") the animation works as expected--> <div id="notification" style="visibility; hidden;"> 121 </div>

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

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