【问题标题】:Is there a cross-browser way to ignore opacity when using jquery's clone()?使用 jquery 的 clone() 时是否有一种跨浏览器的方法来忽略不透明度?
【发布时间】:2009-08-21 23:43:55
【问题描述】:

我在我的文档中使用表格,我希望能够让用户将新项目提交到列表,然后让它“自动”出现在列表顶部(是的,这会更容易使用 DIV,但使用我所拥有的)。

我正在使用 jQuery 和 clone() 创建最新表格行的副本,然后在我更新后使用 fadeIn() 显示新项目并将其添加到列表顶部。因为在内部 jQuery 将元素(假设为 DIV)转换为“块”,所以我还将 css 类更改为“表行”。它工作正常。

完整的代码在这里:

    var row = $("tbody tr:first").clone().hide(); // clone and then set display:none
    row.children("td[class=td-date]").html("today");
 // set some properties
    row.children("td[class=td-data]").html("data");
    row.children("td[class=td-type]").html("type");
// fadeIn new row at the top of the table.
    row.insertBefore("tbody tr:first").stop().fadeIn(2000).css("display","table-row"); 

问题在于,如果我太快地运行该过程 - 即在 fadeIn 完成之前,“clone()”命令最终也会克隆不透明度。

通过调整上面的第一行,我实际上可以让它在 Firefox 中工作:

 var row = $("tbody tr:first").clone().css("opacity","1").hide();

我现在担心的是,我不确定是否可以有效地完成这些工作,和/或“不透明度”是否可以安全地依赖于跨浏览器。

以前有没有人做过类似的事情,并且可以提供任何关于更可靠方法的指示?

【问题讨论】:

    标签: javascript jquery clone effects


    【解决方案1】:

    opacity 作为 jQuery css 属性是安全的跨浏览器,因为它消除了实现中的浏览器差异。这是来源

    // IE uses filters for opacity
    if ( !jQuery.support.opacity && name == "opacity" ) {
      if ( set ) {
        // IE has trouble with opacity if it does not have layout
        // Force it by setting the zoom level
        elem.zoom = 1;
    
        // Set the alpha filter to set the opacity
        elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) +
        (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
      }
    
      return elem.filter && elem.filter.indexOf("opacity=") >= 0 ?
      (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '': "";
    }
    

    以下作品。 Working Demo - 将 /edit 添加到 URL 以使用它。

      // stop previous animation on the previous inserted element
      var prevRow = $("tbody tr:first").stop(true,true);
    
      var row = prevRow.clone();
      row.children("td.td-date").text("today");
      row.children("td.td-data").text("data");
      row.children("td.td-type").text("type");
    
      row.fadeIn(2000).prependTo("tbody");
    

    【讨论】:

      【解决方案2】:

      没有理由在你的克隆上使用 hide。克隆尚未添加到 dom 中,因此它不可见。

      试试这个:

      var row = $("tbody tr:first").clone(); // clone
      // set some properties
      row.children("td[class=td-date]").html("today");
      row.children("td[class=td-data]").html("data");
      row.children("td[class=td-type]").html("type");
      // fadeIn new row at the top of the table.
      row.insertBefore("tbody tr:first").fadeOut(0).fadeIn(2000).css("display","table-row");
      

      【讨论】:

        【解决方案3】:

        如果你这样做,我认为 jQuery 会处理它。

        var row = $("tbody tr:first").clone().fadeIn(0).hide();
        

        【讨论】:

        • 在 Firefox 中不起作用,目前 css("opacity",1) 似乎是最好的选择,但还没有在 IE 上测试过。
        猜你喜欢
        • 2011-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-14
        • 2011-05-22
        • 1970-01-01
        • 1970-01-01
        • 2011-09-12
        相关资源
        最近更新 更多