简体   繁体   English

CSS 触发器 animation 而不是使用 hover

[英]CSS trigger animation instead of using hover

Just have a quick question about how would I trigger CSS animation which is currently set for hover to be automatically executed.只是有一个快速的问题,关于我如何触发 CSS animation,它当前设置为 hover 自动执行。

I have tried applying the keyframe and the animation however the animation is already finished and is not transitioned although the transition is set correctly.我已经尝试应用关键帧和 animation,但是 animation 已经完成并且没有转换,尽管转换设置正确。

The example below only triggers the animation on hover, how would I make it trigger onload?下面的示例只触发 hover 上的 animation,我如何让它触发 onload?

The expected result is an animation to be triggered when the page loads and should trigger only when the element is scrolled into view.预期结果是 animation 将在页面加载时触发,并且应该仅在元素滚动到视图中时触发。

CSS and HTML Code CSS 和 HTML 代码

 main { padding: 5rem 0; display: flex; align-items: center; justify-content: center; } main.carousel { max-height: 500px; overflow: hidden; } main.carousel figure { height: 500px; background: url("https://res.cloudinary.com/dqjhiewf1/image/upload/v1666092821/fullPagePrint/portfolio_fullpage_kg2c1w.png"); width: 550px; background-size: cover; background-position: top center; transform: perspective(1000px) rotateY(-13deg) rotateX(5deg) rotate(1deg) scaleY(0.9) scaleX(0.95) translate(-3%) translateY(-3%); transition: all 8.5s ease, transform 0.5s ease; } main.carousel figure:hover { background-position: bottom center; cursor: pointer; overflow: auto; transform: perspective(1000px) rotateY(0) rotateX(0) rotate(0) scale(1) translate(0) translateY(0); transition: all 8.5s ease, transform 0.5s ease; }
 <main> <a href="https://axie-infinity-lp.vercel.app" target="_blank"> <div class="carousel"> <figure></figure> </div> </a> </main>

View Entire Code on JSFiddle在 JSFiddle 上查看完整代码

ANIMATION ON PAGE LOAD ANIMATION 页面加载

For this you can simply add the javascript code and one id to figure.为此,您只需将 javascript 代码和一个 id 添加到图中即可。 Here in example I added image_figure id and in javascript code I add one class pageEffect to that image_figure在示例中,我添加了image_figure id,在 javascript 代码中,我向该image_figure添加了一个 class pageEffect

Javascript - CSS - HTML Code Javascript - CSS - HTML 代码

 window.onload = function() { document.getElementById('image_figure').className = 'pageEffect'; };
 main { padding: 5rem 0; display: flex; align-items: center; justify-content: center; } main.carousel { max-height: 500px; overflow: hidden; } main.carousel figure { height: 500px; background: url("https://res.cloudinary.com/dqjhiewf1/image/upload/v1666092821/fullPagePrint/portfolio_fullpage_kg2c1w.png"); width: 550px; background-size: cover; background-position: top center; transform: perspective(1000px) rotateY(-13deg) rotateX(5deg) rotate(1deg) scaleY(0.9) scaleX(0.95) translate(-3%) translateY(-3%); transition: all 8.5s ease, transform 0.5s ease; }.pageEffect { background-position: bottom center;important: cursor; pointer:important; overflow: auto;important: transform. perspective(1000px) rotateY(0) rotateX(0) rotate(0) scale(1) translate(0) translateY(0),important. transition; all 8.5s ease, transform 0.5s ease !important; }
 <main> <a href="https://axie-infinity-lp.vercel.app" target="_blank"> <div class="carousel"> <figure id="image_figure"></figure> </div> </a> </main>

View My Code on JSFiddle在 JSFiddle 上查看我的代码

You need to write some javascript code to do that.您需要编写一些 javascript 代码来执行此操作。 Add a class to elements when document loaded.加载文档时向元素添加 class。
https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event https://developer.mozilla.org/zh-CN/docs/Web/API/Window/load_event

Or you can use one of these plugins below:或者您可以使用以下插件之一:
https://wowjs.uk/ https://wowjs.uk/
https://michalsnik.github.io/aos/ https://michalsnik.github.io/aos/

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

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