简体   繁体   English

如何通过javascript实现懒加载animation效果

[英]How to achieve lazy loading animation effect through javascript

I would like to ask everyone.我想问问大家。 I hope to make an effect.希望能起到效果。 When the page is loaded, I can delay the transparency of the background for 2 seconds, and let the duck in the middle hide from the middle of the screen and slowly enlarge and appear.当页面加载时,我可以延迟背景透明度2秒,让中间的鸭子从屏幕中间隐藏起来慢慢放大出现。

But I have just learned javascript and CSS, and now I found a problem that the duck doesn't seem to be enlarged from the center point.但是刚学了javascript和CSS,现在发现一个问题,鸭子好像没有从中心点放大。 I don't know why?我不知道为什么? In addition, I want to trigger the animation with a delay of two seconds after the page is loaded.另外,我想在页面加载后延迟两秒触发 animation。 Is this way of writing OK?这种写法可以吗? Since I'm a beginner, I don't know if this way of writing is correct?由于我是初学者,不知道这样写对不对?

Thanks for watching my question感谢您观看我的问题

 let wrap = document.querySelector('.wrap'); let duck = document.querySelector('.duck'); window.onload = function() { setTimeout(change, 1000); // wrap.style.opacity = "0.6" } function change() { wrap.style.opacity = "0.6"; wrap.transition = "1.2s"; duck.style.transform = "scale(1.6)"; }
 .wrap { position: relative; width: 100vw; height: 100vh; background-image: url("https://images.unsplash.com/photo-1550757750-4ce187a65014?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80"); background-repeat: no-repeat; background-size: cover; background-position: top center; }.duck { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); width: 300px; height: 200px; transform: scale(1); transform-origin: center; transition: 1s; }
 <div class="wrap"></div> <img class="duck" src="https://upload.cc/i1/2022/02/15/UB1kXd.png " alt="">

Try to put the scale of duck to 0, or opacity of duck to 0 from the start, or apply css animation to.duck class. Not sure what are you trying to achieve.尝试从一开始就将鸭子的比例设置为 0,或者将鸭子的不透明度设置为 0,或者将 css animation 应用于.duck class。不确定您要达到什么目的。

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

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