目前你做错了一些小事:
/// this first line basically says run my contents when the DOM is ready
$(function () {
/// this second line says run my contents when the window is fully loaded
/// but you are enabling this only after the DOM has loaded. Depending on
/// the browser these two events can fire in either order so you may find
/// situations where nothing would happen.
$(window).load(function () {
/// then you hide your image div
$('#images-loading').css({
visibility: 'hidden'
})
/// but then ask it to fade in
.fadeTo(200, function () {
});
});
});
编写上述内容的更好方法如下:
/// once the page has fully loaded - i.e. everything including
/// images is ready - then hide our loading message. Set this
/// listener first as it could fire at any time.
$(window).load(function () {
$('#images-loading').stop().fadeOut(200);
});
/// as soon as the DOM is ready, show our loading message
/// the DOM event should fire before window load
$(function(){
/// if you are learning it is far better to name your functions
/// and then reference them where they are needed. It makes things
/// easier to read.
function whenFadeIsComplete(){
alert('fade in complete');
}
/// rather than hide with setting style directly, use jQuery's own hide
/// function. Better still, use a CSS class directly on your #images-loading
/// element that implements `display:none;` or `opacity:0;` - this will
/// render more quickly than JavaScript and mean you avoid your
/// loading div flickering into view before the fade in starts.
$('#images-loading').hide().fadeTo(200, whenFadeIsComplete);
})
如果您尝试上述方法,它可能会解决您的问题,因为它仅在您强制刷新页面时才有效。但是,您可能会发现,如果图像被缓存在浏览器中,您将看不到加载消息(因为页面加载太快) .. 强制浏览器刷新将清除缓存(在大多数浏览器中)并给出您的消息是时候再次展示了。