【问题标题】:IE / Edge CSS3 Transitions not working with background-position propertyIE / Edge CSS3 过渡不适用于背景位置属性
【发布时间】:2017-04-05 07:23:52
【问题描述】:

我知道我说的是 IE,但我认为 transition 属性已在较新的(Edge?)版本中得到修复。

我正在尝试转换元素的background-position(请参见下面的演示),但无论出于何种原因,它在 IE 中都不起作用... :'-(

在下面的演示中,“点击我”按钮应该突出显示相邻的文本,但对于 IE 则不然。

如果您切换“切换显示”按钮,您会看到显示背景颜色(当然是在转换之后),因为切换 display:none 会强制重新绘制。我知道background-size 在IE 中可能不是transition-able,但这会影响我在background-position 上的单一转换属性吗?有什么方法可以让这种背景颜色从左到右过渡(在 IE 以外的任何其他浏览器中查看演示以获得所需的效果)?

谢谢!

/** JS only to facilitate the test and trigger the CSS on button click **/
var myHighlighter = document.querySelector('.button');
var myToggle = document.querySelector('.toggle');

myHighlighter.addEventListener('click', press)
// toggling `display:none` just forces a repaint
myToggle.addEventListener('click', toggle);

function press(e) {
  var newState = (e.target.getAttribute('aria-pressed')!=="true");
  e.target.setAttribute('aria-pressed', newState);
}

function toggle(e) {
  var isToggled = (e.target.previousElementSibling.getAttribute('style'))
  if (isToggled) {
    e.target.previousElementSibling.removeAttribute('style');
  } else {
    e.target.previousElementSibling.setAttribute('style', 'display:none;');
  }
}
/* conditional CSS for transition based on presence of `aria-pressed="true"` attribute */
.button + .text {
  background: linear-gradient(to right, orange 50%, transparent 50%) no-repeat;
  background-position:right bottom;
  background-size: 200% 100%;
  transition: none;
}

.button[aria-pressed="true"] + .text {
  background-position: left bottom;
  transition: background-position .5s linear;
}
<div class="wrapper">
  <button type="button"
        class="button"
        aria-pressed="false"> 
        Click Me
  </button>

  <span class="text">IE won't transition the background-position (color highlighting) initially without toggleing the display (repaint)</span>
  <button class="toggle">Toggle Display</button>
  
</div>

【问题讨论】:

  • 哪个版本的IE?转换在 IE

标签: css internet-explorer css-transitions microsoft-edge


【解决方案1】:

将过渡属性移到第一组,如下所示:

.button + .text {
background: linear-gradient(to right, orange 50%, transparent 50%) no-repeat;
background-position:right bottom;
background-size: 200% 100%;
transition: background-position .5s linear;
}

.button[aria-pressed="true"] + .text {
background-position: left bottom;
}

抱歉,我不知道如何将您的 sn-p 添加到我现有的答案中...

添加到您的 CSS:

.text {
display:block;
}

这使它在 IE 中工作。这是您编辑的代码: https://jsfiddle.net/kellyking/2zLhz8t6/2/

【讨论】:

  • 谢谢,但这似乎无法为我解决问题,您可以将我的 sn-p 复制到您的答案并更新吗?另外,我不希望在返回原始状态时进行转换,这就是为什么我在第一个 css 块上有transition:none
  • “IE Edge”是什么意思?没有这样的事情。
  • 我的意思是,发帖人问的是“我知道我在谈论 IE,但我认为过渡属性在最近的(Edge?)版本中已修复。”
  • 感谢@Kelly,display:block 确实修复了它 :-) 不需要移动过渡的另一部分(也许删除那部分?)
  • @Kelly,我刚刚更新了您的答案并删除了第一部分,因为它没有必要。简单地说,inline 元素在 IE 中是不可转换的,因此需要在我的 span 上显示块。再次感谢!
猜你喜欢
  • 2011-08-11
  • 2012-05-23
  • 2014-04-01
  • 1970-01-01
  • 2013-12-04
  • 2018-02-25
  • 1970-01-01
相关资源
最近更新 更多