August 17, 2010

resize DIV matching the new content with jquery

Question by Sandro Antonucci

I have a DIV which shows one image at once coming from an ajax script, the images are all different in height and are showed with fadeIn/fadeOut (just for the tag)

How can I allow to resize the DIV (that contains the img tag of course) “sliding” based on the new content before the images that fadein changes the DIV height very rudely? 😛

Thanks

Answer by Starx

Well do Something like this

$(".change").click(function() {
  $(".mydiv").fadeOut('slow');
  $(".myimg").attr("src",'');    
  $(".myimg").attr("src","linktonewimage.jpg");
  var newheight = $(".myimg").height();    
  $(".mydiv").css("height",newheight+"px");
  $(".mydiv").fadeIn('slow');
});

I am assuming you have a <img> tag with myimg class inside a div with mydiv class.

Check the DEMO HERE

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!