开发者

jQuery Cycle plugin - Automatic height for containers

开发者 https://www.devze.com 2023-01-06 15:37 出处:网络
I\'m using the cycle plugin on some divs, like this: <div class=\"se开发者_如何转开发ctions\">

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

<div class="se开发者_如何转开发ctions">
  <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.


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号