简体   繁体   中英

How to make addEventListener sync

when window loads div with a class preload doesnt disappear with transition 0.5s; it disappears immediately. I want to first disappear with transition 0.5s then add display none

window.addEventListener('load', () => {
      $('.preload').css({
         'opacity': '0',
         'transition': '0.5s'
      });
   });
   $('.preload').css('display': 'none');

I think this is the shortest way to achieve that. fadeOut is a jquery method, which creates an animation, for fade effect, and sets display to none , when the element gets faded completely. The parameter is the time for transition in milliseconds .

Check fadeOut here : fadeOut

 $(window).on('load', () => { $('.preload').fadeOut(500); });
 html, body{ margin: 0; } .preload { height: 200px; width: 100vw; background: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="preload"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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