我知道我在谈论IE,但我认为 transition
属性已在更近期(Edge?)版本中得到修复。
我尝试转换元素的background-position
(请参阅下面的演示),但无论出于何种原因,它都无法在IE中运行......:&#39 ;-(
在下面的演示中,"点击我"按钮应该突出显示相邻的文本,但不适用于IE。
如果您切换"切换显示"按钮,您将看到背景颜色显示(当然过渡后),因为切换display:none
正在强制重绘。我知道background-size
可能不是transition
- 能够在IE中使用,但是这会影响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>
&#13;
答案 0 :(得分: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;
}
抱歉,我无法弄清楚如何将您的代码段添加到我现有的答案中......
添加到您的CSS:
.text {
display:block;
}
这使它在IE中运行。这是您编辑的代码: https://jsfiddle.net/kellyking/2zLhz8t6/2/