如何在具有滚动条的屏幕中心显示loading.gif

时间:2014-07-25 17:17:53

标签: jquery css

每当用户点击按钮时,我都会尝试显示loading.gif /旋转轮。 我在母版页中添加了如下所示的div:

<div id="loadingDiv">
    <div>
        <h7>Please wait...</h7>
    </div>
</div>

这是css:

#loadingDiv{
  position:absolute;
  top:0px;
  right:0px;
  width:100%;
  height:100%;
  background-color:#666;
  background-image:url('ajax-loader.gif');
  background-repeat:no-repeat;
  background-position:center;
  z-index:10000000;
  opacity: 0.4;
  filter: alpha(opacity=40); /* For IE8 and earlier */
}

当我尝试这个时,loading.gif显示在页面的中心。但由于页面很长,我必须滚动才能看到loading.gif。

如何在屏幕中央显示loading.gif? 如果我向下滚动,我应该再次在屏幕中央看到它。

4 个答案:

答案 0 :(得分:34)

这是解决方案。 。 JS FIDDLE DEMO

HTML

<body>
<div class="content">
    Long Content Here
    <br/>
    Click to view loading image.
</div>
<div id="divLoading"> 
</div>

CSS

#divLoading
{
display : none;
}
#divLoading.show
{
display : block;
position : fixed;
z-index: 100;
background-image : url('http://loadinggif.com/images/image-selection/3.gif');
background-color:#666;
opacity : 0.4;
background-repeat : no-repeat;
background-position : center;
left : 0;
bottom : 0;
right : 0;
top : 0;
}
#loadinggif.show
{
left : 50%;
top : 50%;
position : absolute;
z-index : 101;
width : 32px;
height : 32px;
margin-left : -16px;
margin-top : -16px;
}
div.content {
width : 1000px;
height : 1000px;
}

的jQuery

$(document).ready(function(){
$("div.content").click(function(){
    $("div#divLoading").addClass('show');
});
});

答案 1 :(得分:25)

在你的CSS中,尝试改变位置:绝对;位置:固定;

答案 2 :(得分:12)

我曾经有过这个html和css,无论我的页面是长还是短,它总是显示在中心位置:

<div id="divLoading" style="margin: 0px; padding: 0px; position: fixed; right: 0px; top: 0px; width: 100%; height: 100%; background-color: rgb(102, 102, 102); z-index: 30001; opacity: 0.8;">
<p style="position: absolute; color: White; top: 50%; left: 45%;">
Loading, please wait...
<img src="images/ajax-loading.gif">
</p>
</div>

FIDDLE EXAMPLE

答案 3 :(得分:1)

变化:

#loadingDiv{position:absolute;top:0px;right:0px;width:100%;height:100%;background-color:#666;background-image:url('ajax-loader.gif'); background-repeat:no-repeat;background-position:center;z-index:10000000;  opacity: 0.4;

要:

#loadingDiv{position:fixed;top:0px;right:0px;width:100%;height:100%;background-color:#666;background-image:url('ajax-loader.gif'); background-repeat:no-repeat;background-position:center;z-index:10000000;  opacity: 0.4;

positionabsolute更改为fixed

<强> JSFiddle Demo