根据背景图像宽高比和固定高度动态更改div大小

时间:2017-06-30 04:01:33

标签: javascript css slider aspect-ratio slick.js

我使用光滑的滑块(http://kenwheeler.github.io/slick/)来显示各种尺寸的图像(纵向和横向图片)。图像显示为div的背景图像属性

但是,滑块具有固定的宽度/高度。

我的要求是让所有光滑的div具有固定的高度 - 但是基于肖像或风景图片的可变宽度。在下面的代码中,图像2,3是纵向模式,1,4是横向模式。

      <div class="wrapper gallery-wrapper">
    <div class="container-fluid gallery-container">
      <div class="row">
        <div class="col-md-12 slideshow">
          <div class="agent-detail-slideshow" id="agent-detail-slideshow">
            <div class="item slick-slide-width" data-image="assets/img/1.png" style="background-image:url('assets/img/1.png')"></div>
            <div class="item slick-slide-width" data-image="assets/img/2.png" style="background-image:url('assets/img/2.png')"></div>

            <div class="item slick-slide-width" data-image="assets/img/3.png" style="background-image:url('assets/img/3.png')"></div>
            <div class="item slick-slide-width" data-image="assets/img/4.png" style="background-image:url('assets/img/4.png')"></div>
          </div>
          <ul>
            <li class="next-carat white shadow"></li>
            <li class="prev-carat white shadow"></li>
          </ul>
        </div>
      </div>
    </div>
  </div>

我将滑块的变量width属性设置为true,并为每个div添加了一个自定义的slick-slide-width类。

什么CSS需要进入光滑滑动宽度?

是否可以仅使用CSS来调整div宽度,或者我是否需要任何javascript来控制div宽度。而且,高度需要固定并且由滑块决定(响应地)。所以,在上面的代码中它将是风景,肖像,肖像,风景div。

1 个答案:

答案 0 :(得分:1)

要根据图像比例调整幻灯片的大小,您需要在幻灯片中使用正确的<img />标记(请注意下面示例中的标记)。并使用......

$('...').slick({
  // ...
  variableWidth: true
  centerMode: true, // optional
  // ...
});

如果您的图像高度不同,您可能希望将其与...结合使用。

.slick-slide img { max-height: /*...*/ }

$(document).ready(function() {
  $('#agent-detail-slideshow').slick({
    infinite: true,
    speed: 300,
    slidesToShow: 1,
    centerMode: true,
    variableWidth: true
  });
})
.slick-slide {
  opacity: .1;
  transition: all .3s cubic-bezier(.4, 0, .2, 1);
  outline: none;
}

.slick-current {
  opacity: 1;
}
.slideshow {
  overflow: hidden;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<link href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick-theme.min.css" rel="stylesheet" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.min.js"></script>


<div class="wrapper gallery-wrapper">
  <div class="container gallery-container">
    <div class="row">
      <div class="col-md-12 slideshow">
        <div class="agent-detail-slideshow" id="agent-detail-slideshow">
          <div class="item"><img src="https://unsplash.it/600/300" /></div>
          <div class="item"><img src="https://unsplash.it/800/300" /></div>
          <div class="item"><img src="https://unsplash.it/400/300" /></div>
          <div class="item"><img src="https://unsplash.it/200/300" /></div>
          <div class="item"><img src="https://unsplash.it/500/300" /></div>
        </div>
      </div>
    </div>
  </div>
</div>