What we have are some images that are vertically center-aligned via jQuery. What we want to happen is to hide the misaligned images (meaning, not centered) on $(document).ready then run the function for centering on window.onload then show the images after that.
The images hide immediately on Firefox and Chrome when the page is loaded. But in IE8, for a brief second, it still shows the misaligned images before hiding them.
Is there a way for IE8 to hide them faster/immediately? Below is the code:
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('.img_mask img').hide();
});
window.onload = function() {
$('.img_mask img').each(function() {
var $img = $(this);
var h = $img.height();
var w = $img.width();
$img.css('margin-top', +h / -2 + "px").css('margin-left',+ w/ -2 + "px");
$('.img_mask img').show();
});
You can't guarantee that javascript will run before things in the page are displayed. You just can't and IE is the worst at it.
You have a couple options to solve your problem:
If you care about sites that don't have javascript, you may also want to display the initially hidden images in a <noscript> tag using a CSS rule in <style> tags
you should hide the img using display:none or visibility:hidden like
.img_mask{
visibility:hidden;
}
afterward show the images using .show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With