2

我正在使用以下代码来显示一个纺车:

$("#loading")
.hide()
.ajaxStart(function(){

    $(this).show();
    setTimeout("3000");
})
.ajaxStop(function(){

    $(this).hide("slow");
})
;   

和:

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

问题:“setTimeout() 不起作用。如何在网页中心显示图像?”

4

4 回答 4

2

setTimeout需要 2 个参数,第一个是callback函数,第二个是timeout毫秒,例如。

setTimeout(funcname,1000);

或者

setTimeout(function(){
    //do stuff
},1000);

在网页中心显示您的图像,您可以使用例如。这种技术

#loading {
    position:absolute;
    top:50%;
    left:50%;
    width:100px;
    margin-left:-50px; //negative half of width
    height:80px;
    margin-top:-40px; //negative half of height
}
于 2012-06-30T17:41:29.977 回答
1
#loading {
   display: none;
   position: fixed;
   top: 48%;
   left: 48%
}

JS

$('#loading').ajaxStart(function(){
   var that = $(this)
    setTimeout(function() {
       that.show();
    }, "3000");
}).ajaxStop(function(){
    $(this).hide("slow");
})
于 2012-06-30T17:40:02.077 回答
1

我不明白你为什么要使用计时器,你可以简单地在 ajax 请求开始时将其打开,然后在成功或错误时将其关闭,例如:

$('#loading').show();

$.post(url, request)

    .success(function(data) {
        $('#loading').hide();
        // Do what you want with data
    })

    .error(function(data) {
        $('#loading').hide();
        // Show error message
    });

要将加载图像放在页面中间,请确保它的容器在 100% 高度的父级内,如果它是嵌套的,请确保其父级都不是position:relative.

#loading {
    position:relative;
    width:100%;
    height:100%;
}

#loading img {
    margin-top: -12px; // Half images height;
    margin-left: -12px; // Half images width;
    position:absolute;
    top:50%;
    left:50%;
}

OR

#loading {
    width:100%;
    height:100%;
}

#loading img {
    width:24px // Images width
    height:auto; // If Image size changes don't mess with proportions
    margin:0 auto;
    margin-top:48%;
}
于 2012-06-30T17:47:34.870 回答
0

您以错误的方式使用 SetTimeout 函数

设置超时(函数(){

// 做某事

 }, 3000);

于 2012-06-30T17:39:54.357 回答