我正在创建一个Flickity幻灯片,其中的图像应该适合包含的元素,这样就不需要滚动来查看所有图像。目前我有它,所以图像填充了可用的宽度,但你可以垂直滚动。
我在jsFiddle上创建了一个我想要做的事情的例子。我也在使用LazySizes插件,并在Picturefill中使用srcset
和sizes
,但我不认为这个问题与任何一个有关,因为问题似乎是Flickity通过内联样式向flickity-viewport
元素添加了一个高度,该高度是根据图像的自然高度而不是缩放高度计算的。
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;
}
}
CSS的其余部分来自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);
};
})();
Flickity文档似乎建议我可以在CSS中设置100%的高度,但每当我在CSS中添加高度或使用setGallerySize: false
时,我的图库都没有高度。
我不确定这是否是你想要的答案,但你可以在父容器(在这种情况下是主体)上设置一个高度,这样你的幻灯片的100%高度实际上就会有一个高度。
你可以在这里看到一个例子。http://jsfiddle.net/jm82g4yr/
在这种情况下,我添加了这个css
body, html {
height: 100%;
}
Flickity启动时,请尝试选项adaptiveHeight
。它帮助了我。