deleteRule CSSKeyframesRule method confusing behaviour in IE11

半城伤御伤魂 提交于 2019-12-06 11:14:08
  1. What is the proper way (basically, 'Am I doing something wrong?') to use deleteRule in the IE (or if another method should be used)?

  2. Why am I not able to delete more than two rules out of five?

    The first MSDN link does not apply; that deleteRule() method applies to top-level rules, not keyframe rules.

    The text "The key must resolve to a number between 0 and 1, or the rule is ignored." from the second link is actually taken from the 2013 WD of css-animations, and means that instead of a string containing the 0%-100% keyframe selector, Internet Explorer expects a decimal number representing the percentage. The argument does not represent an index.

    So for a 0% keyframe rule, IE expects the value 0; for a 100% keyframe rule, IE expects the value 1; and for a 33.3% keyframe rule, IE expects the floating-point value 0.333:

    key_frame_list.deleteRule(0)     // Deletes the 0% keyframe rule
    key_frame_list.deleteRule(0.333) // Deletes the 33.3% keyframe rule
    key_frame_list.deleteRule(1)     // Deletes the 100% keyframe rule
    

    Once a 0% rule has been deleted, if no 0% rules remain then additional calls to deleteRule(0) will do nothing.

    And since keyframes cannot exceed 100%, deleteRule(2) is meaningless since it would mean deleting a 200% keyframe rule, which cannot exist.

  3. Is there a method suitable for this purposes that will work with the same arguments on Firefox, Chrome and IE11, I am not aware of?

    No; Internet Explorer 11 follows the 2013 WD (itself having been developed between 2012 and 2013 following the release of Internet Explorer 10), meaning its implementation is not consistent with the current standard, in which the deleteRule() method has been changed to accept a string argument instead of a numeric argument.

    This means the API is incompatible, and so there is no clean workaround. You'll just have to attempt both arguments. I changed the following statement in your fiddle:

    // delete old rules
    for(var i = 0; i < to_be_removed_key_list.length; i++) {
      key_frame_list.deleteRule(to_be_removed_key_list[i])
    }
    

    to:

    // delete old rules
    for(var i = 0; i < to_be_removed_key_list.length; i++) {
      try {
        key_frame_list.deleteRule(to_be_removed_key_list[i])
      } catch (e) {
        key_frame_list.deleteRule(+(parseFloat(to_be_removed_key_list[i]) / 100).toFixed(3))
      }
    }
    

    The +(parseFloat(to_be_removed_key_list[i]) / 100).toFixed(3) bit converts a percentage string to a numeric value taking rounding errors into account. The rounding errors inherent to IEEE-754 floating-point numbers is the reason the API was changed in the first place (that, and consistency with the appendRule() method which has always expected a string), except since it was only changed some time after Internet Explorer 11 was released, and since IE11 will no longer receive platform updates, this means IE11 is stuck with its old WD implementation (which, I must emphasize, was current at the time of its development).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!