Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
113
rated 0 times [  115] [ 2]  / answers: 1 / hits: 76373  / 12 Years ago, fri, july 20, 2012, 12:00:00

My web page is like the following:



<div id=id1 class=stuff>
TEXT, FORMS, and STUFF
</div>

<div id=id2 class=stuff style=display:none>
TEXT, FORMS, and STUFF
</div>

<div id=id3 class=stuff style=display:none>
TEXT, FORMS, and STUFF
</div>

<a id=btn1>DD</a>
<a id=btn2>DD</a>
<a id=btn3>DD</a>


Under this I have jQuery click events which set the display of the clicked item to inherit and the others to none.



$(#btn2).click(function (e) {
$(#id1).css('display','none');
$(#id3).css('display','none');
$(#id2).css('display','inherit');
});


The showing and hiding works correctly however I do notice that some things in the initially hidden divs do not render correctly, especially the elements that get manipulated by CSS. Essentially when the page loads the hidden divs do not correctly get rendered and when they are shown things look ugly. What is the way to properly do this?



EDIT::::::::::::::::::::::::::::::::::::::::::::::



What I ended up doing is setting all of the initially hidden divs to visibility: none, then in the pages onLoad() event setting the display: none. When I toggle I change both the visibility and display. Everything renders correctly and because things are statically set to not visible there is no ugly 2 seconds where all the divs show.


More From » jquery

 Answers
14

Try using visibility instead. Example:



$(#id2).click(function (e) {
$(#id1).css('visibility','hidden');
$(#id3).css('visibility','hidden');
$(#id2).css('visibility','visible');
});


Both display and visibility can have an effect on browser behavior.



An alternative work-around to both is to set the opacity of the divs you want to hide to 0. That always works in my experience but is less elegant.






Update in reply to comment: In that case, you can set other properties like the width and height to 0px and the over-flow to hidden so that the divs don't occupy any space on screen. Ugly, but basic, and works.



<style>
.hidden {
visibility: hidden;
overflow: hidden;
width: 0px;
height: 0px;
}
</style>

<div class=hidden><img src=http://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/Aster_Tataricus.JPG/245px-Aster_Tataricus.JPG/></div>
<div class=hidden><img src=http://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Chamomile%40original_size.jpg/280px-Chamomile%40original_size.jpg/></div>
<div><img src=http://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Jonquil_flowers06.jpg/320px-Jonquil_flowers06.jpg/></div>


You can use the jQuery addClass and removeClass methods to make the divs visible and invisible, e.g.: $(#id1).removeClass(hidden); and $(#id3).addClass(hidden);.


[#84119] Thursday, July 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
danar

Total Points: 271
Total Questions: 94
Total Answers: 93

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
;