There are div
elements with child p
elements that repeatedly go from hidden to shown states. Both display
and opacity
attributes are used for this. The opacity
attribute has a 1 second transition time.
The cycle is as follows:
- Change
display: none
todisplay: initial
- Wait 50 milliseconds for document reflow (which should happen on its own)
- Change
opacity: 0
toopacity: 1
- Wait 2 seconds
- Change
opacity: 1
toopacity: 0
- Wait 1 second for transition to finish
- Change
display: initial
todisplay: none
- Wait 1 second
- Repeat
This works perfectly fine in FireFox, but in Google Chrome it only works in certain cases, demonstrated in the code snippet.
- In the simple case it doesn't work.
- If the child
p
is set toinline-block
it works. - If the
inline-block
element is a grandchild instead of a child it doesn't work. - If the position of the child is set to
fixed
it works.
The curious thing is that the transition in step 5 also fails, which has nothing to do with the display
attribute, so this does not seem to be due to reflow failures.
var flag = true;
var elements;
window.onload = init;
function init() {
elements = document.getElementsByClassName("foo");
cycleQuotes();
setInterval(cycleQuotes, 2000);
}
function cycleQuotes() {
if(flag) {
for(var i = 0; i < elements.length; i++) {
elements[i].style.display = "initial";
}
setTimeout(function() {
for(var i = 0; i < elements.length; i++) {
elements[i].style.opacity = 1;
}
},50);
} else {
for(var i = 0; i < elements.length; i++) {
elements[i].style.opacity = 0;
}
setTimeout(function() {
for(var i = 0; i < elements.length; i++) {
elements[i].style.display = "none";
}
},1000);
}
flag = !flag;
}
.foo {
display: none;
opacity: 0;
transition: opacity 1s;
}
.inline-block {
display: inline-block;
}
#fixed {
position: fixed;
left: 100px;
top: 0px;
}
<div class="foo">
<p>normal</p>
</div>
<div class="foo">
<p class="inline-block">inline-block</p>
</div>
<div class="foo">
<div>
<p class="inline-block">inline-block but grandchild</p>
</div>
</div>
<div class="foo">
<p id="fixed">fixed</p>
</div>