简体   繁体   English

jQuery的模块化AJAX加载指示器

[英]Modular AJAX loading indicator with jQuery

I am trying to show a loading image (and turn the parent transparent) for a page with many AJAX requests. 我正在尝试为包含许多AJAX请求的页面显示加载图像(并使父图像透明)。 What would be the most efficient way to add/remove the loading icon and opacity for each div independently? 独立添加/删除每个div的加载图标和不透明度的最有效方法是什么?

Here is what I have so far. 这是我到目前为止所拥有的。 The problem with my approach is that the opacity is applied to the gif too, which is not what I want. 我的方法的问题在于,不透明度也应用于gif,这不是我想要的。 Is there an easy fix to my code or a better approach to this? 是否有对我的代码的简单修复或更好的方法?

Example: http://jsfiddle.net/justincook/x4C2t/ 示例: http//jsfiddle.net/justincook/x4C2t/

HTML: HTML:

<div id="a" class="box">A</div>
<div id="b" class="box">B</div>
<div id="c" class="box">C</div>

JavaScript: JavaScript的:

$.fn.loading = function(show){
    if(show){
        this.prepend('<div class="loading-centered"></div>');
        this.css({ opacity: 0.3 });
    }else{
        this.children('.loading-centered').remove();
        this.css({ opacity: 1 });
    }
};

$('#a').loading(true); //start  
setTimeout(function(){ $('#a').loading(false); }, 3000); // stop

I would keep the styling in the CSS and just use JS to inject / remove the element http://jsfiddle.net/x4C2t/7/ 我将样式保留在CSS中,仅使用JS注入/删除元素http://jsfiddle.net/x4C2t/7/

$.fn.loading = function(show){
    if(show){
        this.prepend('<div class="loading-centered"></div>');
    }else{
        this.children('.loading-centered').remove();
    }
};

css: CSS:

.box {
    float:left;
    width:100px;
    height:100px;
    border:1px solid #bbb;
    margin: 10px;
    padding:20px;
    text-align:center;
    border-radius:10px;
    background: #eee;    
    position: relative;
}

.loading-centered {
    background:url('http://www.ajaxload.info/images/exemples/24.gif') center center no-repeat white;
    position:absolute;
    height: 100%;
    width: 100%;
    top: 0;
    left: 0;
    border-radius:10px;
    zoom: 1;
    filter: alpha(opacity=50);
    opacity: 0.5;
}

Opacity applies to an elements children as well. 不透明度也适用于儿童元素。 Instead of using opacity just make the background of the parent have more opacity with rgba colors. 而不是使用不透明度,只需使用RGBA颜色使父级背景具有更大的不透明度即可。 Here is an example. 是一个例子。 Also you were using 你也在用

setInterval(function(){ $('#a').loading(false); }, 3000);

When you should have done 什么时候应该做

setTimeout(function(){ $('#a').loading(false); }, 3000);

That was causing the page to crash for me. 那导致页面崩溃了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM