Solved

Jquery, Maintaining height of parent div while switching child

Posted on 2009-07-07
3
815 Views
Last Modified: 2012-05-07
I have a page were about 500px down I use jquery to fade in divs of different sizes. They are all set to display:none except a separate class applied only for the latest news item, which is displayed on page load.

Right now what i do is hide both classes for the divs and then immediately afterward fade in the unique id that has been clicked from the news item menu to the left. Visually it looks great but for that split of a second there is no div maintaining height and if you are scrolled down and the parent shrinks that causes the page to kick back up to the top.

Is there a way to make the function do this: get the height of the visible div, apply it to the parent, then hide the children and fade in the unique id and finish the function with removing the height that was just set on the parent? I imagine this would solve the page jumping back up.

Much appreciated for any thoughts!
// the pertinent php/html
 
// this is were the left newsitem menu is created and the function to fade in the main div on the right is located
 
$i = 0;
 
foreach ($posts as $post)
{    
     $i++;
	 
     echo '<div ';  if($i&1) { echo 'class="newsitem"';} else {echo 'class="newsitem_2"';} echo '><a href="#newsitem-' , $post['id_topic'] , '" id="newsitem-' , $post['id_topic'] , '-' , $post['id_msg'] , '" class="clickable"><strong>' , $post['subject'] , '</strong><br /><br />' , $post['time'] , '</a></div>
 
// the jquery function I need help with
	 
	 <script type="text/javascript">
jQuery().ready(function(){	
	$(\'a#newsitem-' , $post['id_topic'] , '-' , $post['id_msg'] , '\').click(function() {	
    $(\'.right600_viewer_hidden\').hide();
	$(\'.right600_viewer_visible\').hide();																				   
	$(\'#newsitem-' , $post['id_topic'] , '\').fadeIn(\'slow\');	
    return false;
    });
});	
</script>
';
}
 
 
 
 
// the pertinent css
 
// this is the parent for the div display area
 
.container600_right {	
	width:600px;			
	float:left;	
}
 
// these are the children
 
.right600_viewer_visible {
	width:575px;	
	background:#494949;
	color:#999999;
	font-size:13px;
	padding:10px 10px 20px 10px;	
}
 
.right600_viewer_hidden {
	width:575px;	
	background:#494949;
	color:#999999;
	font-size:13px;
	padding:10px 10px 20px 10px;
	display:none;	
}

Open in new window

0
Comment
Question by:aiwazz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
Pravin Asar earned 500 total points
ID: 24808306
for the container(s), specify the min-height  style attribute


                  <a href="#" onclick="$('#div1').show();">Show Div1</a>&nbsp;&nbsp;&nbsp;&nbsp;
                  <a href="#" onclick="$('#div1').hide();">Hide Div1</a>&nbsp;&nbsp;&nbsp;&nbsp;
                  <a href="#" onclick="$('#div2').show();">Show Div2</a>&nbsp;&nbsp;&nbsp;&nbsp;
                  <a href="#" onclick="$('#div2').hide();">Hide Div2</a>
                  <div style="min-height: 400px; background-color: #4499dd;">
                  <div id="div1" style="display: none;">This is div 1</div>
                  <div id="div2" style="display: none;">This is div 2</div>
                  </div>
0
 

Author Comment

by:aiwazz
ID: 24957714
Figured out a nice way to do it. I agree min height would work, but if they're scrolled down and the container was quite variant in size it would jump up then regrow. So I grab the height of the container before I hide it, set the parents height to that, fade in the new content, then remove the height attribute from the container. Much smoother. Thanks
<script type="text/javascript">
jQuery().ready(function(){	
	$(\'a#newsitem-' , $post['id_topic'] , '-' , $post['id_msg'] , '\').click(function() {
	oldh = $(\'#news_r\').height();
	$(\'#news_r\').height(oldh);
        $(\'.right600_viewer_hidden\').hide();
	$(\'.right600_viewer_visible\').hide();																				   
	$(\'#newsitem-' , $post['id_topic'] , '\').fadeIn(\'slow\', function() {
		if(jQuery.browser.msie) this.style.removeAttribute(\'filter\');
		$(\'#news_r\').css({height: ""});
	});	
    return false;
    });
});	
</script>

Open in new window

0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 24960429
Thanks for posting you specific solution.

Also for the Points/Grade.
0

Featured Post

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javascript duplicates 9 25
How does this modal work? 3 33
carousel control extend past the images 9 28
Where does this error come from? 8 23
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question