代码之家  ›  专栏  ›  技术社区  ›  user113716

在内联块的元素上使用jQuery remove()时出现问题

  •  1
  • user113716  · 技术社区  · 14 年前

    我有一行水平的div元素 display:inline-block

    我可以添加元素,行就会展开。当我通过将宽度设置为0来移除元素时,线不会保留它的单行外观,而是强制第二条线在动画期间临时出现。

    IE、Webkit和Firefox中的行为是相同的。

    我可以改变接口来避免这个问题,但是我仍然想知道是什么导致了它,如果可能的话,如何修复它。

    body {
        text-align: center;
    }
    #container {
        height: 20px;
        border: 1px dashed #AAA;
        background: #EEE;
        margin: 0 auto;
        display:table !important;
        display: -moz-inline-stack;  /* Pre FF3 fix */
        display: inline-block;
        zoom: 1;                    /* IE fix */
        *display: inline;           /* IE fix */
    }
    .item {
        cursor: pointer;
        width: 50px;
        height: 18px;
        border: 1px solid purple;
        vertical-align: top;
        display: inline-block;
        color: white;
        vertical-align: top;
        display: -moz-inline-stack;  /* Pre FF3 fix */
        display: inline-block;
        zoom: 1;                    /* IE fix */
        *display: inline;           /* IE fix */
    }
    .outer {
        background: orange;
    }
    
    
    $('#add').click(function() {
        $(this).before('<div class="item"></div>')
    });
    
    $('#add').click().click().click()
    
    $('.item:not(.outer)').live('click', function() { 
        $(this).animate({width: 1, paddingLeft: 0}, 1000, function() {$(this).remove()}); 
    });
    
    <div id="container"><div class='item outer'></div><div id="add" class="item outer">Add</div></div>
    
    2 回复  |  直到 14 年前
        1
  •  2
  •   user113716    14 年前

    好吧,如果有人感兴趣的话,这是我的解决方案。

    在jQuery中 animate: 函数,在 if() 在检查“height”和“width”的语句中,我添加了以下内容:

    // Detect inline-block standard, pre ie8 fix, and pre Firefox 3 fix
    opt.ibCheck =(jQuery.css(this,'display') === 'inline-block') || 
                (jQuery.css(this,'zoom') !== 'normal' && jQuery.css(this,'display') === 'inline') || 
                (jQuery.css(this,'display') === '-moz-inline-stack');
    

    jQuery.fx.prototype.update: ,在 如果() 如果() 声明如下:

    if ( ( this.prop === "height" || this.prop === "width" ) && this.elem.style && !this.options.ibCheck ) {
    

    …正在检查“animate”中设置的ibCheck属性。

    一切似乎都很顺利。它检查内联块、常见的firefox3之前的补丁或ie8之前的补丁。如果找到一个,它会将ibCheck设置为“true”。这个 update() 如果ibCheck为“true”,函数不会将显示更改为“block”。

    对于IE,可能有更好的检查方法。我想如果有人设置了一个“inline”元素的动画,并且将“zoom”设置为“normal”以外的任何值,可能会导致问题?至于我,直到昨天我才知道IE甚至接受了“缩放”属性!

        2
  •  1
  •   Community Ramakrishna.p    7 年前

    This other question 也许能给我们一些启示