简体   繁体   English

让动画延迟到视口中?

[英]Make animations delay until in viewport?

I'm working on a site where intro text will be displayed based off time of day.我正在一个网站上工作,其中介绍文本将根据一天中的时间显示。 It will switch between good morning, good evening, etc. I found a way using CSS to delay each word to fade in one by one.它将在早上好、晚上好等之间切换。我找到了一种使用 CSS 来延迟每个单词一个一个淡入的方法。 The issue is that the words are not in viewport right off the bad, so the animation is happening before the viewer sees it.问题是这些文字并没有立即出现在视口中,因此动画在观众看到它之前就已经发生了。 How do I delay is until it's in viewport?我如何延迟直到它在视口中? Here is my current code including the CSS that made the animations enter one by one:这是我当前的代码,包括使动画一一输入的 CSS:

 var time = new Date().getHours(); if (time >= 1 && time <= 12) { document.getElementById("time").innerHTML = "<span>☀️</span> <span>Good</span> <span>Morning,</span> <span>Creative.</span>" } else if (time > 12 && time <= 17) { document.getElementById("time").innerHTML = "<span>☀️</span> <span>Good</span> <span>Afternoon,</span> <span>Creative.</span>" } else { document.getElementById("time").innerHTML = "<span>🌙</span> <span>Good</span> <span>Evening,</span> <span>Creative.</span>" }
 #time span { opacity: 0; transform: translateY(30px); animation: time 0.5s ease-out forwards; display: inline-block; } #time span:nth-of-type(2) { animation-delay: 0.5s; } #time span:nth-of-type(3) { animation-delay: 0.5s; } #time span:nth-of-type(4) { animation-delay: 1s; } @keyframes time { to { opacity: 1; transform: translateY(0); } }
 <h4 id="time">Hello Creative</h4>

Is there some way to clarify viewport in CSS?有什么方法可以澄清 CSS 中的视口吗? Or does this require JS?或者这需要JS吗? Thanks for any help!谢谢你的帮助!

Unfortunately there is no way to detect when something is in view with CSS.不幸的是,没有办法用 CSS 检测到什么时候出现在视图中。 You should look into Intersection Observer API, then apply the animation CSS class when the item observed is in view.您应该查看 Intersection Observer API,然后在观察到的项目在视图中时应用动画 CSS 类。

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

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