Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Cycle plugin - Automatic height for containers

I'm using the cycle plugin on some divs, like this:

<div class="sections">
  <div class="Section"> <img /> text </div>
  <div class="Section"> <img /> text </div>
  <div class="Section"> <img /> text </div>
</div>

all the .section divs have variable height, based on their contents. How can I make cycle not to resize the container div (sections) to the largest child height? Instead I want the container to resize on every animation to the current child height.

like image 805
Alex Avatar asked Jul 08 '10 12:07

Alex


3 Answers

ok, I managed to do it by hooking a function on 'after' event:

function onAfter(curr, next, opts, fwd) {
  var $ht = $(this).height();

  //set the container's height to that of the current slide
  $(this).parent().animate({height: $ht});
}

found the info here: http://forum.jquery.com/topic/jquery-cycle-auto-height

like image 169
Alex Avatar answered Nov 19 '22 00:11

Alex


set containerResize option to 0 and try. More option details here.

like image 37
Teja Kantamneni Avatar answered Nov 19 '22 01:11

Teja Kantamneni


I've done it a little bit different:

  $('.cycle-list').cycle({
    containerResize: 0,
    before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
      var container = $(this).parent();
      container.css('height', Math.max(container.height(), $(nextSlideElement).height()));
    },
    after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
      $(this).parent().css('height', $(this).height());
    }
  });

My items had different height as well. In the before, it makes sure the container is big enough for the bigger of the 2 slides. In the after, it just resizes to the next slide. This way it never over-flows because your container is too small.

note: this is for cycle 1

like image 32
SpadXIII Avatar answered Nov 18 '22 23:11

SpadXIII