Why does all content get jerked downwards before fading in the following, and how can i fix it?
Using FireFox 3.6.3, thanks in advance.
<html>
<head>
<script type="text/javascript" language="javascript" src="http://localhost/javascript/jquery-1.4.2.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$("#button").click(function(){
$("*").fadeTo("slow",0.0);
});
});
</script>
</head>
<body>
<p>Just a normal paragraph doing my job!</p>
<p>Me too!</p>
<input type="button" id="button">
</body>
</html>
It has something to do with trying to fade all elements, including those outside the
<body>. Try:But why would you want to fade every single element, when you can simply do a fade on the body itself.
Edit: Some more research shows that when trying to fade the
<style>and<head>elements, in no particular order, causes everything to move down. Don’t know why yet, but you can see an example here – http://jsfiddle.net/UKn8r/2/Edit 2: Ok, I think I may have a reason here. The
<head>and its children elements such as<style>,<script>, etc. are by default set todisplay: nonein the user agent’s stylesheet. When fading them out, jQuery ends up setting their display property todisplay: block. Now the contents of these child elements are not meant to be displayed on the screen, but by setting them todisplay: block, it gets displayed as a horizontal block about 20px high with no content, which shifts everything else downwards. Note that if you were to empty out the<script>element and make theonclickinline, then you wouldn’t see the jump on Firefox since the element will be empty and not consume any space on screen even when displayed as a block. So changing it to:will not cause any jumps.
Also, your original code verbatim, will work properly on Webkit browsers (Chrome, Safari) as the
displaystyle property for<script>elements does not get overridden asblock. For these browsers, however, if you were to have astyleelement with some content inside it, then you would see the same behavior as<style>will have an inline style attribute havingdisplay: block. Now it may seem utterly useless to have something like,<style style="display: block; opacity: 0">..</style>, but this is just an explanation for why you’re seeing the behavior that you’re seeing. So to reproduce the same problem on these browsers, try this code:The
<style>property must have some content, and not pure whitespace, so I put the junkp {}there.This concludes my wasteful search for something that shouldn’t be done in the first place 🙂