【问题标题】:jQuery overlay -- Same code for different DIVsjQuery 覆盖——不同 DIV 的相同代码
【发布时间】:2013-02-08 02:30:33
【问题描述】:

我正在尝试执行以下操作 --> 我有一个动态数量的 DIV 元素 .. 全部像这样堆叠:

 __________________________________________
|                                          |
|                                          |
|                    DIV1                  |
|                                          |
 __________________________________________
 __________________________________________
|                                          |
|                                          |
|                    DIV2                  |
|                                          |
 __________________________________________
 __________________________________________
|                                          |
|                                          |
|                    DIV3                  |
|                                          |
 __________________________________________

我在每个按钮的内部都有一个按钮,它调用相同的 jQuery 函数:

function updateProduct(old_q,ent_id,element){
        var new_q = jQuery(element).prev().children('input').val();
        jQuery.post('/ajax_calls/checkout/update_cart_item.php', {entity_id: ent_id,old_q: old_q,new_q: new_q}, function(data) {
                cartItemOut = data;
                alert(cartItemOut);
        });
}

如您所见,我使用 jQuery 来确定 THIS 元素是什么,然后找到与子/父等相关的输入。我的问题是,我如何在整体上覆盖“加载”DIV 任一 DIV1, DIV2 DIV3 给出我上面用来确定我在哪个DIV 的代码?下面是每个DIV的基本结构:

<div class="item_wrapper">
        <div style="float:left; width:100px; height:100px; margin-left:15px; position:relative;">
                <div style="margin-top:100%;"></div>
                <img width="100" height="69" src="/images/100/198.jpg" style="position:absolute; top: 0px; bottom: 0px; left: 0px; right: 0px; max-height:100%; max-width:100%; display:block; margin:auto;">
        </div>
        <div style="float:left; width:198px; margin-left:20px; margin-bottom:7px;">
                <div style="height:31px; overflow:hidden; margin-bottom:2px;">
                        <span style="font-size:12px; font-weight:bold;">Name</span>
                </div>
                <br> 
                <span style="color:#86a057;">item#: </span>198<br>
                <span style="color:#86a057;">price:</span>
                <span style="color:#000; font-weight:bold;">$107.99</span><br>

                <div style="width:300px; margin-top:10px;">

                       <div style="float:left; margin-top:4px; width:20px; margin-right:3px; color:#86a057;">qty: </div>

                       <div style="float:left; margin-right:15px;">
                       <input type="text" value="1" style="font-family:arial; font-size:13px; height:17px; margin-top:2px; width:30px; text-align: center;"></div>

                       <div onclick="updateProduct('1','198',this);" style="float:left; margin-right:7px; margin-top:3px;  width:59px; height:20px; cursor:pointer;">
                       <img src="/skin/frontend/templat/default/images/update_cart.png">
                       </div>

                       <div style="float:left; margin-right:7px; margin-top:3px;  width:59px; height:20px; cursor:pointer;">
                       <img src="/skin/frontend/template/default/images/remove_cart.png">
                       </div>

                </div>


        </div>
        <div class="clearfix"></div>
</div>

所以本质上,问题是我有多个DIVs 类item_wrapper。当我点击updateProduct() 时,如何用覆盖和加载器图像覆盖那个 DIV?我尝试的一切似乎都是徒劳的,我开始认为这在简单领域是不可能的。我什至尝试过检测item_wrapper 的位置和WIDTH/HEIGHT 并在其上定位FIXED DIV 一样复杂,但这似乎不是正确的解决方案,必须做一个更好的方法。

【问题讨论】:

    标签: jquery css overlay loader


    【解决方案1】:

    您可以使用以下 jQuery 找到父 div:

    var itemWrapper = jQuery(element).parents('.item_wrapper');
    

    然后您可以将一些加载元素附加到该项目并相应地定位和调整大小。

    function updateProduct(old_q, ent_id, element) {
        var new_q = jQuery(element).prev().children('input').val();
        jQuery.post('/ajax_calls/checkout/update_cart_item.php', {entity_id: ent_id,old_q: old_q,new_q: new_q}, function(data) {
                cartItemOut = data;
                alert(cartItemOut);
                $(element).find('.loading-overlay').remove();
        });
        var $ = jQuery, itemWrapper = $(element).parents('.item_wrapper'),
            iHeight = itemWrapper.height(), iWidth = itemWrapper.width();
        itemWrapper.prepend($('<div class="loading-overlay"><span>Loading...</span></div>').height(iHeight).width(iWidth));
    }
    

    看起来另一个答案已经提到了这一点,但是您需要使用绝对定位来覆盖您的元素。

    .loading-overlay {
        position: absolute;
        background: gray;
        z-index: 1;
        opacity: 0.5;
    }
    

    这是一个使用类似于你所拥有的东西的小提琴:http://jsfiddle.net/Jeff_Meadows/dyPMG/3/

    【讨论】:

    • 这真的很接近我的需要。非常感谢,它帮助我指明了正确的方向!
    【解决方案2】:

    您可以使用绝对定位来屏蔽元素。您可以增强下面给出的示例。

    <div class="ct">
        <div class="x">
            <input type="button" Value="Test" />
        </div>
        <div class="x">
            <input type="button" Value="Test" />
        </div>
        <div class="x">
            <input type="button" Value="Test" />
        </div>
    </div>
    
    $('.ct').on('click', '.x input', function(e){
        var target = $(e.target);
        var div = target.closest('.x');
        div.append('<div class="mask">mask</div>');
        setTimeout(function(){
            $('.mask', div).remove();
        }, 2500);
    });
    
    .ct {
        padding: 10px;
    }
    
    .x {
        height: 50px;
        margin-bottom: 10px;
        border: 1px solid lightgrey;
        position: relative;
    }
    
    .mask {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        opacity: .5;
        background-color: lightblue;
    }
    

    演示:Fiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 2012-02-24
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      相关资源
      最近更新 更多