【发布时间】:2018-06-30 21:56:26
【问题描述】:
有没有一种方法可以创建居中的owl-carousel,其中每个图像具有相同的高度而不会丢失其纵横比?
我尝试使用JS/jQuery 计算图像大小,但随后owl-carousel 的计算变得一团糟。与CSS s 的结果相同。示例(jsfiddle):
$(document).ready(function () {
$('.loop').owlCarousel({
center: true,
items: 3,
loop: true,
margin: 20
});
});
.owl-carousel .owl-stage {
height: 150px;
}
.owl-carousel .owl-item {
height: 100%;
}
.owl-carousel .owl-item .item {
height: 100%;
}
.owl-carousel .owl-item img {
height: 100% !important;
width: auto !important;
margin: 0 auto;
}
<link rel="stylesheet" type="text/css" href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.carousel.min.css">
<link rel="stylesheet" type="text/css" href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.theme.default.min.css">
<script type="text/javascript" src="https://owlcarousel2.github.io/OwlCarousel2/assets/vendors/jquery.min.js"></script>
<script type="text/javascript" src="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/owl.carousel.js"></script>
<div class="loop owl-carousel owl-theme">
<div class="item"><img src="http://via.placeholder.com/320x460/ff0000/000000?text=IMG 01" /></div>
<div class="item"><img src="http://via.placeholder.com/460x320/00ffff/000000?text=IMG 02" /></div>
<div class="item"><img src="http://via.placeholder.com/100x80/00ff00/000000?text=IMG 03" /></div>
<div class="item"><img src="http://via.placeholder.com/50x100/ff00ff/000000?text=IMG 04" /></div>
<div class="item"><img src="http://via.placeholder.com/70x80/ffff00/000000?text=IMG 05" /></div>
</div>
当前的div/image应该是
- 在旋转木马的中心,
- 与所有其他项目具有相同的高度
- 保持纵横比和
- 与上一项或下一项并排。
如果元素比视口大,那么它就会被切割。
有没有办法通过owl-carousel 实现这一目标?
我检查了其他一些关于 SO 的类似问题,但据我所知,它们都有不同的重点:
-
Image slider: maintaining equal height for all images while keeping slider responsive
// overrides the aspect ratio -
Owl Carousel align each item vertically when their height not equal
// not the same height of each item*
编辑:我只是仔细检查了auto-height demo...
启用使用
autoHeight: true。目前仅适用于屏幕上的 1 个项目。计划是计算所有可见项目并根据最高项目更改高度。
好像还没有这样的选项。
【问题讨论】:
-
如果所有图片的高度相同 - 请尝试添加
autoWidth: true -
我刚刚在您评论帖子前几分钟进行了编辑。
autoWidth: true仅适用于屏幕上的一项,s。 jsfiddle.net/wittich/06kzfdq9 -
你说
autoWidth,但你在小提琴里写了autoHeight。 -
@MariyanPetrov 你是对的,很抱歉混淆了
autoWidth和autoHeight。实际上它似乎按我的意愿工作:jsfiddle.net/wittich/1Lsx482c (如果你想获得荣誉,你可以将 jsfiddle 作为答案发布,我会将其标记为正确答案)。 -
作为答案发布。
标签: javascript jquery css owl-carousel equal-heights