淘汰赛 beforeRemove 和 CSS 动画不能在 IE 中一起工作

我有一个使用 Knockout foreach 来填充一些行的表。长话短说,视图模型会定期更新新数据,并且从该表中删除不再有效的任何行。

使用 beforeRemove 绑定,然后应用 CSS 动画来淡出行。这在 Chrome 和 Firefox 上运行良好,但由于某种原因在 IE11 中不起作用。如果在 beforeRemove 之外使用,动画的完全相同的代码将在 IE 11 中工作。我在下面的代码段中汇总了我的代码的简化版本。

关于为什么 IE 有这个问题的任何想法?谢谢!

var item = function(name) {
  this.name = ko.observable(name);
}

$('document').ready(function () {
  function queueViewModel() {
    var self = this;
    self.queueItems = ko.observableArray([
      new item("one"),
      new item("two"),
      new item("three"),
      new item("four")
    ]);
    self.removeItem = function(item) {
      self.queueItems.remove(item)
    }
    self.beforeRemoveFadeOut = function (element) {
        $(element).addClass('animated fadeOutUp')
        .one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
            //Actual code removes an element here
            $(element).remove();
        });
    }
  }
  var queueViewModel = new queueViewModel();
  ko.applyBindings(queueViewModel,document.getElementById('wrapper'));
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.1/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="wrapper">
  <table>
    <thead>
      <th>Item Name</th>
      <th>Remove Item</th>
    </thead>
    <tbody data-bind="template: { foreach: queueItems, beforeRemove: beforeRemoveFadeOut }">
      <tr>
        <td><span data-bind="text:name"></span></td>
        <td><a href='#' data-bind="click: $parent.removeItem">Remove</a></td>
      </tr>
    </tbody>
  </table>
</div>

用户3297291

当您转换表格行元素时,IE11似乎存在一个已知问题......当我尝试在 IE11 中转换表格行时,它根本不显示任何内容:

在此处输入图片说明

table {
  background: red;
}

tr {
  transform: translate3d(50px, 0, 0);
  background: green;
}
<table>
  <tbody>
    <tr>
     <td>test</td>
   </tr>
 </tbody>
</table>

不知何故,这会导致未加载完整的动画。反过来,这会导致animationEnd事件永远不会触发。

我建议你:

  1. 在您的标记中使用其他 HTML 元素(<li><div>等),或
  2. 使用不使用transform内部的过渡您可以在动画库的源代码中查找这些内容。

我找不到太多关于表行转换“错误”的信息……但是我测试了这两种替代方法,它们确实解决了您的问题。

var item = function(name) {
  this.name = ko.observable(name);
}

$('document').ready(function () {
  function queueViewModel() {
    var self = this;
    self.queueItems = ko.observableArray([
      new item("one"),
      new item("two"),
      new item("three"),
      new item("four")
    ]);
    self.removeItem = function(item) {
      self.queueItems.remove(item)
    }
    self.beforeRemoveFadeOut = function (element) {
        $(element)
          .one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
            $(element).remove();
          })
          .addClass('animated fadeOutUp');
    }
  }
  var queueViewModel = new queueViewModel();
  ko.applyBindings(queueViewModel,document.getElementById('wrapper'));
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.1/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="wrapper">
  
    <ul data-bind="template: { foreach: queueItems, beforeRemove: beforeRemoveFadeOut }">
      <li class="">
        <span data-bind="text:name"></span>
        <a href='#' data-bind="click: $parent.removeItem">Remove</a>
      </li>
    </ul>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章