views:

598

answers:

3

I have a JQuery function that toggles one of the divs in my layout. The problem is that the whole layout moves around on the screen when the div appears or disappears. Is there a way to achieve the same show/hide effect without altering the layout?

<script>
  $(document).ready(function(){
    $("button").click(function () {
      $("#layoutDiv").toggle();
    });
  });
</script>
+6  A: 

You could wrap that div in another div that maintains the proper width/height to keep the layout consistent.

<div style="width:300px;height:200px">
  <div class="animateMe">
    <p>When I close, the outer div will still be there.</p>
  </div>
</div>

If you don't know the height/width before hand, you could set it programmatically with javascript when the page loads. Just get the .width() and .height() values of the collapsible div and apply them to the css of the outer div.

Jonathan Sampson
I use that myself but it's not too semantic-friendly..
dalbaeb
@dalbaeb - what do you mean by 'semantic-friendly'? I mean, I know what it means, but in this context?
karim79
Did the job. Thanks.
ipso facto
You're welcome, Ipso.
Jonathan Sampson
+4  A: 

Try writing a new function that uses visibility: hidden instead of display: none. That will maintain the CSS box (and the layout) without showing the div.

Stefan Mai
I initially thought the same thing. But what if he wants to see the div animate close/open - that would cause some problems.
Jonathan Sampson
A: 

Jonathan Sampson's answer will work - alternatively without knowing what exactly you're doing (the markup, and the required effect) I would use absolute positioning with a high z-index, which will take it out of document flow. This is what modal windows / lightboxes generally use. Here's a tutorial.

Flemish Bee Cycle