javascript - 加载 Gif 页面不加载背景图像

标签 javascript jquery html css loader

我有一个 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" );

谢谢

最佳答案

<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/

关于javascript - 加载 Gif 页面不加载背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25044543/

相关文章:

javascript - 使用 AngularJS 折叠时如何切换按钮上的图标?

html - 特定类的表奇偶 tr backgroundcolor

html - 我们可以覆盖 html5 输入类型 url 或 email 中内置的默认验证吗

html - 边框半径打破字体很棒?

java - 在 Java 中运行 JavaScript 的最佳方式是什么?

javascript - 使用 Bootstrap 3,如何将 html 添加到工具提示?

javascript - 从 FileReader() 返回字节数组

jquery - CSS z-index 不工作

点击后Javascript禁用按钮,但表单未提交

jquery - IE8 Bootstrap Respond.JS,列表项不是水平的