admin管理员组文章数量:1432566
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();
});
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();
});
Share
Improve this question
asked Aug 29, 2011 at 4:55
catandmousecatandmouse
11.8k24 gold badges95 silver badges158 bronze badges
7 Answers
Reset to default 3You 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:
- Use a CSS stylesheet rule that makes the images hidden initially and then use javascript to display only the aligned ones.
- Don't put the mis-aligned images in the DOM - only put aligned ones in. This might require changing how your page HTML works. The images could be in a javascript array and you load them via JS and only insert ones that will align.
- Hide the image container until you've run your javascript and weeded out the mis-aligned images.
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()
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();
}
}
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 ...
http://api.jquery./ready/
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?
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./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.
$(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.
try other methods to speed selector image.
$('.img_mask').find('img').hide();
本文标签: javascriptjQuery hide() method doesn39t immediately take effect in IE8Stack Overflow
版权声明:本文标题:javascript - jQuery hide() method doesn't immediately take effect in IE8? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745606738a2665879.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论