【问题标题】:Loading Gif Page Does not load background images加载 Gif 页面不加载背景图片
【发布时间】:2014-07-30 18:44:11
【问题描述】:

我有一个 gif,在我的网站加载时出现,但是 gif 消散器和页面在所有内容加载后出现,除了通过 css 调用的背景图像。

我怎么能让 gif 只有在背景图像加载后才消失。

我正在为加载程序使用此代码:

html:

<html class="loading">
    <!-- All the things -->
</html>

css:

html {
    -webkit-transition: background-color 1s;
    transition: background-color 1s;
}
html, body {
    /* For the loading indicator to be vertically centered ensure */
    /* the html and body elements take up the full viewport */
    min-height: 100%;
}
html.loading {
    /* Replace #333 with the background-color of your choice */
    /* Replace loading.gif with the loading image of your choice */
    background: #333 url('loading.gif') no-repeat 50% 50%;

    /* Ensures that the transition only runs in one direction */
    -webkit-transition: background-color 0;
    transition: background-color 0;
}
body {
    -webkit-transition: opacity 1s ease-in;
    transition: opacity 1s ease-in;
}
html.loading body {
    /* Make the contents of the body opaque during loading */
    opacity: 0;

    /* Ensures that the transition only runs in one direction */
    -webkit-transition: opacity 0;
    transition: opacity 0;
}

javascript

// IE10+
document.getElementsByTagName( "html" )[0].classList.remove( "loading" );

// All browsers
document.getElementsByTagName( "html" )[0].className.replace( /loading/, "" );

// Or with jQuery
$( "html" ).removeClass( "loading" );

谢谢

【问题讨论】:

  • css中的背景在哪里定义?
  • 像这样:#homepage { background: url(/wp-content/uploads/2014/07/home.jpg) fixed; background-repeat:no-repeat; -webkit-background-size:cover; -moz-background-size:cover; -o-background-size:cover; background-size:cover; background-position: bottom; overflow: auto; margin: 0 auto; width: 100%; position: relative; padding: 0 0 0 0; height: 100vh; }
  • 你的控制台说什么?是否显示任何错误?这可能只是错误的路径。
  • 是否有工作示例或页面的实时视图?
  • 一种方法是使用window.onload 或jquery 的$(window).load,当页面上的所有内容都加载完毕时会触发。

标签: javascript jquery html css loader


【解决方案1】:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $('.splash').fadeTo(555,0, function() {
        $(this).remove();
    });
});
</script>
</head>
<body>
<div class="splash" style="position: absolute; width: 100%; height: 100%; background-image:url(mygif); background-repeat: repeat;">&nbsp;</div>
    <!-- All the things -->
</body>
</html>

测试过! => http://jsfiddle.net/3m3S8/

【讨论】:

  • $(document).ready 在加载 dom 时触发,而不是在包含所有图像的页面。
  • 谢谢,但这并没有达到我想要的效果。当大部分页面加载时,gif 是可见的,但页面仍然在没有背景的情况下加载。
  • 这主要是为了展示淡化和去除元素的原理。您可以通过绑定要等待的图像的负载来应用相同的原则
猜你喜欢
  • 2016-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
  • 2017-08-17
  • 2015-08-24
  • 1970-01-01
相关资源
最近更新 更多