简体   繁体   English

使元素在CSS中的动画延迟期间保持隐藏

[英]Make element remain hidden during animation-delay in CSS

I have an element that slides across the screen from what I would like to look like off the page and then slide and "land" on the opposite side. 我有一个元素,该元素从我希望在页面外看起来像在屏幕上滑动,然后滑动并“降落”在另一侧。 I've got the animation down but it seems that during the delay the image just sits on the page in waiting. 我放慢了动画,但似乎在延迟过程中,图像只是在等待页面上。 The delay is because I have another animation that needs to finish before this one starts. 延迟是因为我有另一个动画需要在此动画开始之前完成。

here is my CSS code and I am referencing it via class with a react front end 这是我的CSS代码,我通过带有React前端的类引用它

.tester{
  position: relative;
  animation-name: test_css_moving_sideways;
  animation-duration: 3s;
  animation-iteration-count: 1;
  animation-direction: linear;
  left: 90%;
  animation-delay: 2s;  
}


@keyframes test_css_moving_sideways {
  0% { left: 0px; top: 0px;}
  100% { left: 90%; top: 0px; }
}

1) To fix this you can set left property in the css so that it sits at 0% so then when your animation starts it doesnt have to move to a left of 0% (where your animation starts) because it will already be there :) 1)要解决此问题,您可以在css中设置left属性,使其位于0%,这样,当您的动画开始播放时,它不必向左移动0%(动画开始播放的位置),因为它已经存在: )

2) Add animation-fill-mode - this controls what happens at the end of the animation. 2)添加animation-fill-mode -控制动画结束时发生的情况。 Setting this value to forwards will apply the css from the end of the animation to the object 将此值设置为forwards会将动画结束时的CSS应用于对象

So amend your css to: 因此,将您的CSS修改为:

.tester{
  position: relative;
  animation-name: test_css_moving_sideways;
  animation-duration: 3s;
  animation-iteration-count: 1;
  animation-direction: linear;
  left: 0%;
  animation-delay: 2s;
  animation-fill-mode: none, forwards;
}

Check codepen: https://codepen.io/sasssssha/pen/Nayzpg 检查Codepen: https ://codepen.io/sasssssha/pen/Nayzpg

You should create one more class (.testerAnimated) and add this classname to element with a js function(if you want animation in a certain time) or you can add it at once(in this case animation starts when page will be loaded). 您应该再创建一个类(.testerAnimated),然后使用js函数将此类名添加到元素中(如果您希望在特定时间动画),或者可以一次添加(在这种情况下,动画将在加载页面时开始)。 Check it: 核实:

  .testerAnimated{
  animation-name: test_css_moving_sideways;
  animation-duration: 4.5s;         //play with it
  animation-delay: 2s;  
  }    

  .tester{
  position: relative;
  animation-name: test_css_moving_sideways;
  left: 90%;
  visibility:hidden;  
}


@keyframes test_css_moving_sideways {
  0% { left: 0px; top: 0px;visibility:visible;}
  100% { left: 90%; top: 0px;visibility:visible;}
}

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

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