Problem with fadeOut from jQuery!

Asked

Viewed 51 times

0

Guys, I’m having a little problem here and I’ve tried to solve it in a lot of ways but nothing... I am trying to make a PRELOADER that will appear before loading the page the problem is that even after the page is loaded it still does not disappear.. I have the following code

HTML:

<div id="preloader"></div>

CSS:

#preloader{
  position: fixed;
  left: 0;
  top: 0;
  z-index: 999;
  width: 100%;
  height: 100%;
  background: $white url("../img/loading.gif") no-repeat center center;
  background-size: 80px;
}

jQuery:

$(window).load(function() {
  $("#preloader").delay(500).fadeOut("slow").remove();
});
  • try just $('#preloader').fadeOut("slow");

  • I’ve tried in a long time...

2 answers

1

You are calling the wrong callback in the wrong window event, try the same as this fiddle here (I made some simple modifications to not use URL): https://jsfiddle.net/5t014jy0/

$(document).ready(function() {
  $("#preloader").delay(500)
  .fadeOut("slow", function() { 
    return $("#preloader").remove() 
  })
});
  • Guy the way you passed worked out.. but he shows the page while an image is still loading, I put a heavy image to test.. I’ll even minify it. There’s some solution?

  • It’s a matter of adjusting the timing of your delay I believe, or using Lucas' answer

1


You can use the event document.onreadystatechange. When it is triggered just check the status if it is complete, hides the #preloader, following example:

document.onreadystatechange = function () {
   if (document.readyState == "loading") {
      //Mantem o #preloader visivel
   }else if(document.readyState == "complete") {
      $("#preloader").fadeOut("slow");
   }
}
  • 1

    Cool brother, worked the way expected. thank you!

  • For nothing my friend!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.