Reputation: 11809
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();
});
Upvotes: 3
Views: 2922
Reputation: 35
try other methods to speed selector image.
$('.img_mask').find('img').hide();
Upvotes: -1
Reputation: 8919
$(document).ready(function() {}
The document.ready() function works just as the name implies. Document refers to the DOM, or Document Object Model, while in this case “ready” refers to when the DOM is registered by the browser. So your code will wait till every thing is ready and starts working on it. Thats why you're facing some delay.
Upvotes: 0
Reputation: 31033
you should hide the img using display:none
or visibility:hidden
like
.img_mask{
visibility:hidden;
}
afterward show the images using .show()
Upvotes: 1
Reputation: 45252
Does it work if you embed the script into the HTML, immediately after the last item is declared?
<head>
<script type="text/javascript">
window.onload = function() {
// Image aligning code goes here
}
</script>
</head>
<body>
<div class='img_mask'>
<img src="http://www.hollywoodgo.com/wp-content/uploads/2010/04/smurfs-movie-225x300.jpg"
</div>
<script type="text/javascript">
$('.img_mask img').hide();
</script>
</body>
This works for me, but I don't have IE8 installed to test it on.
I would argue that it is better to have the images visible in the HTML and hide them through the script (as you are doing), rather then to have them hidden in HTML and revealed through the script.
If the rendering device has scripting disabled, it is better to see the images badly aligned then not at all.
Upvotes: 0
Reputation: 707436
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
Upvotes: 3
Reputation: 171
You could use the .ready function to hide it on load and then display the other stuff following that?
Look into the jquery .ready() function ...
Kind of like 3nigma and.. jfriend said..., set the element to have an initial display: none; and then have it show in an event or delay... could help smooth things out?
Upvotes: 0
Reputation: 2597
Your logical need to be improved. You should hide the img
first before it appended to DOM
, waiting for the condition meets then show it.
$('hiddenimg').hide().appendTo($(body));
window.onload = function () {
if (/*some conditions*/) {
$('hiddenimg').show();
}
}
Upvotes: 0