加载页面时动画GIF不动画

时间:2011-04-29 07:44:52

标签: javascript onload gif animated

我有一个使用asp.net(C#)生成服务器端的页面。加载页面需要一段时间,因为它最多有100个iframe。我想在页面加载时显示“请等待”动画gif,所以我有以下内容:

<style type="text/css">
    #blackout
    {
       filter:alpha(opacity=70); 
       -moz-opacity:0.7; 
       opacity:0.7; 
       position:absolute;
       background-color:#222233;
       z-index:50; 
       left:0px;
       top:0px;
       right:0px;
       bottom:0px;
       width:102%;
       height:102%;
    }
    #loading
    {
       filter:alpha(opacity=100); 
       -moz-opacity:1; 
       opacity:1; 
        position:fixed;
        text-align:center;
        background-color:#EEEEEE;
        top:50%;
        left:50%;
        margin-top:-55px;
        margin-left:-75px;
        width:150px;
        height:75px;
        z-index:100; 
    }
    </style>
</head>
<body onload="document.getElementById('loading').style.visibility='hidden';document.getElementById('loading').style.display='none';document.getElementById('blackout').style.visibility='hidden';document.getElementById('blackout').style.display='none';">
    <div id="blackout">
        <div id="loading"><img id="loadinggif" src="graphics/loading.gif" alt="" style="margin-top:12px; "/><br />Please wait...</div>
        <script type="text/javascript" language="javascript">
            document.getElementById('loadinggif').src = 'graphics/loading.gif';
        </script>
    </div>
    <form id="form1" runat="server">

所以问题是加载gif没有移动。我试过用

setTimeout("document.getElementById('loadinggif').src = 'graphics/loading.gif'", 100);

也使得gif移动“有点”,但不是预期的。

如何在加载过程中使其动画不受干扰?

5 个答案:

答案 0 :(得分:12)

旧问题,但发布给其他googlers:

Spin.js适用于此用例: http://fgnass.github.com/spin.js/

答案 1 :(得分:2)

我遇到了类似的问题并找到了一个非常简单的解决方案 - 我的标记看起来有点像这样:

<li><a href="#" onclick="this.parentNode.className = '_indicator'; doSomething();">Foo</a></li>

CSS-Class将动画gif分配给该li元素的背景。这很好用,但图像没有移动。之后将“focus()”应用于li解决了这个问题:

<li><a .. onclick="this.parentNode.className = "_indicator'; this.parentNode.focus(); ...

答案 2 :(得分:0)

setTimeout(function(){document.getElementById('loadinggif').src = 'graphics/loading.gif'}, 100);

将其作为一项功能包含在内并观看您的报价

你可能想这样做。在html中声明src但隐藏它,然后执行:

setTimeout(
  function() {
    document.getElementById('loadinggif').style.display ='block';
       setTimeout(
          function() {
             document.getElementById('loadinggif').style.display = 'none';
          }, 100);
  }, 100);

答案 3 :(得分:0)

你试过setInterval()吗? setTimeout只运行一次,而setInterval连续运行直到停止。

var x = setInterval(document.getElementById('loadinggif').src = 'graphics/loading.gif', 100);

......完成加载后....

clearInterval(x);

虽然,现在我想到了,你的所有代码都在做的是将图像源设置为动画gif。真正的问题是IE在后台进行大量的jScript处理并且不运行动画时似乎被“锁定”了。

您可以尝试将动画中的所有gif作为单独的文件,然后使用interfval函数更改src以模仿相同的动画。

也许有点像这样......

//global var
var imgIndex = 0;



var x = setInterval(changeImgSrc, 100);

......完成加载后....

clearInterval(x);


function changeImgSrc(i){
document.getElementById('loadinggif').src = 'graphics/loading' + (imgIndex++) + '.gif'
}

答案 4 :(得分:0)

您可以设置backgroung CSS属性以包含您想要在动画期间显示的gif图像。

background: rgba( 198, 226, 255, .5 ) 
                url('images/15.gif') 
                50% 50% 
                no-repeat;

.5表示不透明度; 在URL部分,您可以为您的gif图像提供相对或完整的URL。

相关问题