Load gif until full page load

爷,独闯天下 提交于 2019-12-10 12:22:24

问题


I'm using this for my site to display a loading gif until my page is loaded.

It's kinda working.

$(window).load(function(){
    $('#overlay').fadeOut();
});

The HTML:

<div id="overlay">
     <img src="loading.gif" alt="Loading" />
     Loading...
</div>

But, the gif disappears to soon. My images aren't fully loaded yet when the loading gif disappears. How do I have to edit this code so it will show the loading gif until EVERYTHING is loaded on my page?


回答1:


Like Arijit said, you can set a timer on the fadeOut() function

DEMO


Or to show a .gif loader until image(s) are ready use this:

$(window).onbeforeload(function(){
//..Your code here..
});

Once the page is loaded and window is ready use this:

$(window).load(function(){
//..Your code here..
});

The load event will only trigger when the page is fully loaded, not just the DOM of the page.

You can also use image placeholders onbeforeload so it only shows placeholders instead of the image(s).

Once image(s) is loaded, placeholders will be replaced with the actual image(s).


My last alternative for you could be this plugin instead. ImagesLoaded

Good Luck!



来源:https://stackoverflow.com/questions/24055493/load-gif-until-full-page-load

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!