Flexslider
  • height's equal the tallest

    2020-02-14 07:28发布

  • 问题:

    I am trying to figure out an issue with Flexslider that only appears when I test it on an mobile browser such as Safari Mobile.

    All of the <li> containers are the same height as the tallest <li>, even when there is not enough content to justify making them equal heights.

    I am not sure how to grab a screen shot of this since it works fine on a desktop browser but not on mobile browsers. Here's a fiddle that shows it working as I would expect when I view it on my desktop browser, the container's height resizes depending on the ammount of content.

    http://jsfiddle.net/CsCyh/

    Here is the hml:

    <div class="flexslider">
      <ul class="slides">
        <li>
          <img src="http://i.imgur.com/YSVlz2Z.jpg" />
          <h2><a href"#">First Link Here</a></h2>
          <p>Some text here that could be a message</p>
          <a href="#" data-role="button" data-icon="arrow-r" data-iconpos="right">Another Link Here</a>
        </li>
        <li>
          <img src="http://i.imgur.com/YSVlz2Z.jpg" />
          <h2><a href"#">Second Link Here</a></h2>
          <p>Some text here that could be a message</p>      
        </li>
        <li>
         <img src="http://i.imgur.com/YSVlz2Z.jpg" />
         <h2><a href"#">Third Link Here</a></h2>
         <p>Some text here that could be a message</p>     
        </li>
      </ul>
    </div>
    

    Here's the JS for flexslider:

    $('.flexslider').flexslider();
    

    回答1:

    Hmm, I ran into this issue awhile ago. I hope this helps!

    var evenSliderHeight = function(slideContainer, slideItem) {
      var slider_height = 0;
      var $slider_slide = $(slideContainer).find(slideItem);
      $slider_slide.each(function() {
        var __height = $(this).outerHeight(true);
        if ( slider_height < __height ) {
           slider_height = __height;
        }
      });
      $slider_slide.css('min-height', slider_height);
    };
    evenSliderHeight('.flexslider-container', '.slide');
    


    回答2:

    $('.flexslider').flexslider({
      after: function(slider){
        currHeight = $('.flexslider').find('.slides > li').eq(slider.currentSlide).outerHeight(true);
        $('.flexslider').height(currHeight);
      }
    });
    

    JSFiddle Update: http://jsfiddle.net/CsCyh/14/




    回答3:

    For anyone still has this issue in 2019/2020, please have a look at the new property called "smoothHeight" at the Flexslider docs

    I fixed my issue by setting "smoothHeight" to true.

    $('.sliders').flexslider({
        animation: 'slide',
        smoothHeight: true,
        controlNav: false,
        customDirectionNav: $('.js-slider-control button'),
    });
    


    回答4:

    Add this to the script that calls the slider.

    animateHeight: false,
    

    eg.:

    $(window).load(function(){ if ( $('#content-slider-wrap').length == 0 ) return false
        $('#content-slider-wrap').flexslider({
            animation:"fade",
            slideshow: true,
            directionNav: true,
            animateHeight: false,
            controlNav:false ,
    ........... etc etc