Every once in a while, Chrome will render perfectly valid HTML/CSS incorrectly or not at all. Digging in through the DOM inspector is often enough to get it to realize the
This seems to do the trick for me. Plus, it really doesn't show at all.
$(el).css("opacity", .99);
setTimeout(function(){
$(el).css("opacity", 1);
},20);
This solution without timeouts! Real force redraw! For Android and iOS.
var forceRedraw = function(element){
var disp = element.style.display;
element.style.display = 'none';
var trick = element.offsetHeight;
element.style.display = disp;
};
call window.getComputedStyle()
should force a reflow
This is my solution that worked for disappearing content...
<script type = 'text/javascript'>
var trash_div;
setInterval(function()
{
if (document.body)
{
if (!trash_div)
trash_div = document.createElement('div');
document.body.appendChild(trash_div);
document.body.removeChild(trash_div);
}
}, 1000 / 25); //25 fps...
</script>
Not sure exactly what you're trying to achieve but this is a method I have used in the past with success to force the browser to redraw, maybe it will work for you.
// in jquery
$('#parentOfElementToBeRedrawn').hide().show(0);
// in plain js
document.getElementById('parentOfElementToBeRedrawn').style.display = 'none';
document.getElementById('parentOfElementToBeRedrawn').style.display = 'block';
If this simple redraw doesn't work you can try this one. It inserts an empty text node into the element which guarantees a redraw.
var forceRedraw = function(element){
if (!element) { return; }
var n = document.createTextNode(' ');
var disp = element.style.display; // don't worry about previous display style
element.appendChild(n);
element.style.display = 'none';
setTimeout(function(){
element.style.display = disp;
n.parentNode.removeChild(n);
},20); // you can play with this timeout to make it as short as possible
}
EDIT: In response to Šime Vidas what we are achieving here would be a forced reflow. You can find out more from the master himself http://paulirish.com/2011/dom-html5-css3-performance/
The previous solutions don't work anymore for me.
I guess browsers optimize the drawing process by detecting more and more "useless" changes.
This solution makes the browser draw a clone to replace the original element. It works and is probably more sustainable:
const element = document.querySelector('selector');
if (element ) {
const clone = element.cloneNode(true);
element.replaceWith(clone);
}
tested on Chrome 80 / Edge 80 / Firefox 75