0

I'm creating a Flickity slideshow where the images should fit the containing element so that there's no scrolling involved to see all the image. At the moment I have it so the images fill the available width but you get vertical scrolling.

I've created an example on jsFiddle of what I'm trying to do. I'm also using the LazySizes plugin and using srcset and sizes with Picturefill, but I don't think the issue is related to any of those as the problem seems to be that Flickity adds a height via inline styles to the flickity-viewport element which is calculated based on the natural height of the image, not its scaled height.

HTML

 <div class="gallery js-flickity" data-flickity-options='{
    "cellAlign": "left",
    "contain": true,
    "wrapAround": true,
    "freeScroll": true,
    "pageDots": false,
    "imagesLoaded": true
  }'>
<div class="gallery-cell">
  <div class="ratio-box">
    <img data-src="http://lorempixel.com/320/238/sports/cell-1a/" data-srcset="http://lorempixel.com/320/238/sports/cell-1a/ 320w,
        http://lorempixel.com/480/357/sports/cell-1a/ 480w,
        http://lorempixel.com/600/446/sports/cell-1a/ 600w,
        http://lorempixel.com/768/571/sports/cell-1a/ 768w" data-sizes="(min-aspect-ratio: 13/9.6) 74.29vh" alt="" class="lazyload">
  </div>
</div>
<div class="gallery-cell">
  <div class="ratio-box">
    <img data-src="http://lorempixel.com/320/238/sports/cell-1b/" data-srcset="http://lorempixel.com/320/238/sports/cell-1b/ 320w,
        http://lorempixel.com/480/357/sports/cell-1b/ 480w,
        http://lorempixel.com/600/446/sports/cell-1b/ 600w,
        http://lorempixel.com/768/571/sports/cell-1b/ 768w" data-sizes="(min-aspect-ratio: 13/9.6) 74.29vh" alt="" class="lazyload">
  </div>
</div>
<div class="gallery-cell">
  <div class="ratio-box">
    <img data-src="http://lorempixel.com/320/238/sports/cell-1c/" data-srcset="http://lorempixel.com/320/238/sports/cell-1c/ 320w,
        http://lorempixel.com/480/357/sports/cell-1c/ 480w,
        http://lorempixel.com/600/446/sports/cell-1c/ 600w,
        http://lorempixel.com/768/571/sports/cell-1c/ 768w" data-sizes="(min-aspect-ratio: 13/9.6) 74.29vh" alt="" class="lazyload">
  </div>
</div>

SCSS

.gallery,
.gallery-cell {
  /* height: 100%; 
  enabling this in conjunction with 
  'setGallerySize: false' means the gallery has no height */
}

.gallery-cell {
  width: 100%;
  visibility: hidden;
}

.flickity-slider > .gallery-cell {
  visibility: visible;
}

.ratio-box {
  height: 0;
  width: 100%;
  padding-bottom: 74.3648961%;
  position: relative;
  img {
    display: block;
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
  }
}

The rest of the CSS comes from flickity.css

JS

window.lazySizesConfig = window.lazySizesConfig || {};
window.lazySizesConfig.loadMode = 1;
window.lazySizesConfig.expand = 222;
window.lazySizesConfig.expFactor = 1.6;

(function() {
var oldFlickityCreate = window.Flickity.prototype._create;

window.Flickity.prototype._create = function() {
  var that = this;
  if (this.element.addEventListener) {
    this.element.addEventListener('load', function() {
      that.onresize();
    }, true);
  }
  this._create = oldFlickityCreate;
  return oldFlickityCreate.apply(this, arguments);
};
})();

The Flickity docs seem to suggest I should be able to set a height of 100% in CSS, but whenever I add a height to the CSS or use setGallerySize: false, my gallery has no height.

Tyssen
  • 1,569
  • 16
  • 35

2 Answers2

0

I'm not sure if this is the answer you're looking for, but you can set a height on the parent container (in this case the body) so that the 100% height of your slideshow will actually have a height.

You can see an example here. http://jsfiddle.net/jm82g4yr/

In this case, I added this css

body, html {
  height: 100%;
}
aptsurdist
  • 19
  • 5
  • Unfortunately in the complete example I'm working on, the gallery isn't a direct child of the `body`. I already have `height:100%` on the body but if I do the same on the gallery's parent element, it still collapses to no height. I think what I'm going to have to do is write some js to add a height to `flickity-viewport` based on the resized image's height. – Tyssen Dec 09 '15 at 03:48
0

Try the option adaptiveHeight when Flickity inits. It helped me.

Arun Kumar Mohan
  • 11,517
  • 3
  • 23
  • 44
Paul Burilichev
  • 406
  • 3
  • 10