CSS CSS3 动画在 IE11 中不起作用,但在其他浏览器中起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34809544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 11:40:59  来源:igfitidea点击:

CSS3 animation is not working in IE11 but works in other browsers

cssanimationcss-animationsinternet-explorer-11

提问by katie hudson

I have created a CSS3 animation on a button. At the moment, it works perfectly everywhere apart from IE. I know it wont work well in older IE versions, but I was atleast expecting it to work in IE11.

我在按钮上创建了一个 CSS3 动画。目前,除了 IE 之外,它在任何地方都可以完美运行。我知道它在较旧的 IE 版本中无法正常工作,但我至少希望它可以在 IE11 中工作。

I have created a fiddle to demonstrate Fiddle

我创建了一个小提琴来演示小提琴

I call the animation on :beforeand :afterlike so

我调用动画:before:after喜欢这样

animation: 1000ms ease 0s normal none infinite running pulse-long;

If you open the fiddle in Firefox or Chrome, you should see the animation on the button working. If you open it in IE11, it is just a static dot. I have gone online and tried a few things, such as moving the animation frames to the top of the CSS file, but it still does not work.

如果您在 Firefox 或 Chrome 中打开小提琴,您应该会看到按钮上的动画正在工作。如果在IE11中打开,它只是一个静态点。我上网尝试了一些东西,例如将动画帧移动到CSS文件的顶部,但它仍然不起作用。

Is there any way to get this animation working in IE11?

有没有办法让这个动画在 IE11 中工作?

回答by Harry

There are two things that are preventing the animation from working in IE11 and they are as follows:

有两件事阻止动画在 IE11 中工作,它们如下:

  • IE11 always has a problem when setting animation-play-stateas runningin the shorthand. There is no justification for this and it should probably be considered as a bug. Fix for this issue should be to just remove the runningfrom the shorthand. This will cause no harm because the default value for animation-play-stateis running.
  • The parent button container's dimension is only 10px x 10px whereas the pseudo-elements are eventually getting a dimension of 60px x 60px during the animation. While other browsers are by defaulting showing the overflow, IE11 seems to be cropping it. This needs to be cross-checked with the specs to find out if it is a bug or something that is loosely defined.
  • The fix for the overflow issue is again pretty simple. Just add a overflow: visiblefor the button container (.btnPulse.inactive). This will also not cause any problem in other browser because they are anyway doing this by default.
  • IE11 设置animation-play-staterunning速记时总是有问题。对此没有任何理由,它可能应该被视为一个错误。解决此问题的方法应该是running从速记中删除。这将导致没有坏处,因为默认值animation-play-staterunning
  • 父按钮容器的尺寸仅为 10px x 10px,而伪元素最终在动画期间获得 60px x 60px 的尺寸。虽然其他浏览器默认显示溢出,但 IE11 似乎正在裁剪它。这需要与规范进行交叉检查,以确定它是错误还是松散定义的东西。
  • 溢出问题的修复再次非常简单。只需overflow: visible为按钮容器 ( .btnPulse.inactive)添加一个。这也不会在其他浏览器中引起任何问题,因为它们默认情况下会这样做。

Snippet showing the overflow problem:

显示溢出问题的代码段:

In the below snippet, I have avoided the animations and just added a couple of default box-shadowto the pseudo-elements such that the whole thing looks like 4 concentric circles with a red colored circle (produced by the button element) in the middle, followed by a white circle (blank space), followed by a blue colored circle (produced by box shadow of :beforeelement) and then a green circle (produced by box shadow of :afterelement).

在下面的代码片段中,我避免了动画,只是box-shadow为伪元素添加了几个默认值,这样整个东西看起来像 4 个同心圆,中间有一个红色圆圈(由按钮元素产生),然后是一个白色圆圈(空白),然后是一个蓝色圆圈(由:before元素的框阴影产生),然后是一个绿色圆圈(由:after元素的框阴影产生)。

In Chrome, Firefox and Opera the concentric circles would be visible completelybut IE11 will show only the center red circlebecause the rest is outside parent's area.

Chrome、Firefox 和 Opera 中,同心圆将完全可见,IE11 将仅显示中心红色圆圈,因为其余部分在父区域之外。

.btnPulse.inactive.throb::before {
  box-shadow: 0 0 0 16px blue inset;
  display: block;
  height: 60px;
  left: -22px;
  margin: 0 auto;
  right: 0;
  top: 50%;
  transform: translate3d(-3px, -50%, 0px);
  width: 60px;
}
.btnPulse.inactive::before {
  border-radius: 100%;
  bottom: -5px;
  box-shadow: 0 0 0 1px red inset;
  content: "";
  display: block;
  height: 30px;
  left: -10px;
  margin: 0 auto;
  position: absolute;
  right: -5px;
  top: -5px;
  transition: all 300ms ease-in-out 0s;
  width: 30px;
}
.btnPulse.inactive.throb::after {
  border-radius: 100%;
  bottom: -5px;
  box-shadow: 0 0 0 8px green inset;
  content: "";
  display: block;
  height: 60px;
  left: -22px;
  margin: 0 auto;
  position: absolute;
  right: -5px;
  top: 50%;
  transform: translate3d(-2px, -50%, 0px);
  transition: all 300ms ease-in-out 0s;
  width: 60px;
}
.btnPulse.inactive {
  background: red none repeat scroll 0 0;
  border: medium none;
  border-radius: 100%;
  height: 10px;
  outline: medium none;
  padding: 0;
  position: relative;
  width: 10px;
}
.btnPulse {
  border-radius: 50%;
  cursor: pointer;
  height: 15px;
  padding: 0;
  transition: all 300ms ease-in-out 0s;
  width: 15px;
}
.btn {
  -moz-user-select: none;
  background-image: none;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
  display: inline-block;
  font-size: 14px;
  font-weight: 400;
  line-height: 1.42857;
  margin-bottom: 0;
  padding: 6px 12px;
  text-align: center;
  vertical-align: middle;
  white-space: nowrap;
}
#button-container {
  position: absolute;
  left: 100px;
  top: 100px;
}
<div id="button-container">
  <button class="btn btnPulse inactive throb"></button>
</div>



Working Snippet with the fix:

修复工作片段:

The below snippet has both the above mentioned fixes applied and it works in IE11, Chrome, Firefox and Opera.

下面的代码段应用了上述修复程序,并且适用于 IE11、Chrome、Firefox 和 Opera。

@keyframes pulse-short {
  100% {
    box-shadow: inset 0 0 0 80px red;
    -moz-box-shadow: inset 0 0 0 80px red;
    -webkit-box-shadow: inset 0 0 0 80px red;
    height: 60px;
    width: 60px;
    left: -22px;
    opacity: 0;
  }
}
@keyframes pulse-long {
  100% {
    box-shadow: inset 0 0 0 10px red;
    -moz-box-shadow: inset 0 0 0 10px red;
    -webkit-box-shadow: inset 0 0 0 10px red;
    height: 60px;
    width: 60px;
    left: -22px;
    opacity: 0;
  }
}
.btnPulse.inactive.throb::before {
  animation: 1000ms ease 0s normal none infinite pulse-long;
  box-shadow: 0 0 0 0 red inset;
  display: block;
  height: 100%;
  left: 3px;
  margin: 0 auto;
  right: 0;
  top: 50%;
  transform: translate3d(-3px, -50%, 0px);
  width: 100%;
}
.btnPulse.inactive::before {
  border-radius: 100%;
  bottom: -5px;
  box-shadow: 0 0 0 1px red inset;
  content: "";
  display: block;
  height: 30px;
  left: -10px;
  margin: 0 auto;
  position: absolute;
  right: -5px;
  top: -5px;
  transition: all 300ms ease-in-out 0s;
  width: 30px;
}
.btnPulse.inactive.throb::after {
  animation: 2500ms ease 300ms normal none infinite pulse-short;
  border-radius: 100%;
  bottom: -5px;
  box-shadow: 0 0 0 0 red inset;
  content: "";
  display: block;
  height: 30px;
  left: -9px;
  margin: 0 auto;
  position: absolute;
  right: -5px;
  top: 50%;
  transform: translate3d(-2px, -50%, 0px);
  transition: all 300ms ease-in-out 0s;
  width: 30px;
}
.btnPulse.inactive {
  background: red none repeat scroll 0 0;
  border: medium none;
  border-radius: 100%;
  height: 10px;
  outline: medium none;
  padding: 0;
  position: relative;
  width: 10px;
  overflow: visible;
}
.btnPulse {
  border-radius: 50%;
  cursor: pointer;
  height: 15px;
  padding: 0;
  transition: all 300ms ease-in-out 0s;
  width: 15px;
}
.btn {
  -moz-user-select: none;
  background-image: none;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
  display: inline-block;
  font-size: 14px;
  font-weight: 400;
  line-height: 1.42857;
  margin-bottom: 0;
  padding: 6px 12px;
  text-align: center;
  vertical-align: middle;
  white-space: nowrap;
}
#button-container {
  position: absolute;
  left: 100px;
  top: 100px;
}
<div id="button-container">
  <button class="btn btnPulse inactive throb"></button>
</div>