简体   繁体   English

可见时激活 CSS 动画

[英]Activate CSS Animation when visible

I am using a CSS animation on my webpage, this animation is in the middle of my page.我在我的网页上使用 CSS 动画,这个动画在我的页面中间。 Its animation starts as soon as I visit the webpage.我一访问网页,它的动画就会开始。 So the user is not able to see it, I have tried few solutions provided on the internet but none of it seems to work for me.所以用户无法看到它,我尝试了互联网上提供的一些解决方案,但似乎没有一个对我有用。

 .pcb-text { display: flex; justify-content: center; align-items: center; } .pcb-text p { font-size: 35px; animation: typing 2s steps(45), blink-caret 0.85s step-end infinite; overflow: hidden; white-space: nowrap; margin: 0 auto; border-right: .12em solid orange; } @keyframes typing { from { width: 0; } to { width: 100%; } } /* The typewriter cursor effect */ @keyframes blink-caret { from, to { border-color: transparent } 50% { border-color: orange; } }
 <div class="pcb-text" id="animated_text"> <div class="text-center"> <p>Some Text Here</p> </div> </div>

I am new to this, so I would really appreciate that if you are giving an answer which includes JS or JQuery, please use a simple method which I can understand我是新手,所以我真的很感激,如果您给出的答案包括 JS 或 JQuery,请使用我能理解的简单方法

You could put the animation in a separate class, and add this class when the element is scrolled into view.您可以将动画放在一个单独的类中,并在元素滚动到视图中时添加此类。

This can be done with jQuery by adding a custom function that detects when the element is in view, and trigger this function every time the user scrolls.这可以通过 jQuery 添加一个自定义函数来完成,该函数检测元素何时在视图中,并在每次用户滚动时触发此函数。

 // function to detect if an element is scrolled into view function isScrolledIntoView(elem) { var docViewTop = $(window).scrollTop(); var docViewBottom = docViewTop + $(window).height(); var elemTop = $(elem).offset().top; var elemBottom = elemTop + $(elem).height(); return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop)); }; // listen for scroll event $(window).scroll(function () { // check if element is scrolled into view if (isScrolledIntoView($('#animated_text'))) { // element is scrolled into view, add animation class $('#animated_text').addClass('animation'); } });
 .pcb-text { display: flex; justify-content: center; align-items: center; } .pcb-text p { font-size: 35px; overflow: hidden; white-space: nowrap; margin: 0 auto; border-right: .12em solid orange; } /* move animation to separate class */ .animation p { animation: typing 2s steps(45), blink-caret 0.85s step-end infinite; } @keyframes typing { from { width: 0; } to { width: 100%; } } /* The typewriter cursor effect */ @keyframes blink-caret { from, to { border-color: transparent } 50% { border-color: orange; } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div style="height: 1000px"></div> <div class="pcb-text" id="animated_text"> <div class="text-center"> <p>Some Text Here</p> </div> </div>

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

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