【问题标题】:Centered owl-carousel where each image has the same hight and keeps its aspect ratio居中的 owl-carousel,其中每个图像具有相同的高度并保持其纵横比
【发布时间】: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 的类似问题,但据我所知,它们都有不同的重点:


编辑:我只是仔细检查了auto-height demo...

启用使用autoHeight: true。目前仅适用于屏幕上的 1 个项目。计划是计算所有可见项目并根据最高项目更改高度。

好像还没有这样的选项。

【问题讨论】:

  • 如果所有图片的高度相同 - 请尝试添加autoWidth: true
  • 我刚刚在您评论帖子前几分钟进行了编辑。 autoWidth: true 仅适用于屏幕上的一项,s。 jsfiddle.net/wittich/06kzfdq9
  • 你说autoWidth,但你在小提琴里写了autoHeight
  • @MariyanPetrov 你是对的,很抱歉混淆了autoWidthautoHeight。实际上它似乎按我的意愿工作:jsfiddle.net/wittich/1Lsx482c (如果你想获得荣誉,你可以将 jsfiddle 作为答案发布,我会将其标记为正确答案)
  • 作为答案发布。

标签: javascript jquery css owl-carousel equal-heights


【解决方案1】:

如果您的所有图像都具有相同的高度 - 尝试添加 autoWidth: true:

$(document).ready(function () {
  $('.owl-carousel').owlCarousel({
      center: true,
      margin:10,
      loop:true,
      autoWidth:true,
      items:4
  })
});
.owl-carousel .owl-stage {
  height: 200px;
}

.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>

【讨论】:

  • 谢谢,这对我有用。关键部分是使用!important 标志作为图像宽度,否则它会被 OwlCarousel 的 CSS 覆盖并且图像会失去它们的纵横比。
猜你喜欢
  • 1970-01-01
  • 2016-05-16
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-07
  • 2017-02-13
相关资源
最近更新 更多