Bootstrap 4 Carousel获取图像大小

时间:2019-08-08 16:52:58

标签: javascript jquery html twitter-bootstrap bootstrap-4

这是一个非常基本的Bootstrap 4 Carousel,带有3张图像。

我实现了两个可用的事件:slide.bs.carouselslid.bs.carousel

我能够在slid事件侦听器中获得图像宽度(当轮播完成其幻灯片过渡时),但是当我尝试在{中获得图像宽度时,图像宽度始终为0 {1}}侦听器(我想知道下一个幻灯片的大小)。

那是为什么?

这是一个代码段。观看控制台。

slide
jQuery('#carouselDemo').on('slide.bs.carousel', function(e) {

  console.log('SLIDE event');
  console.log('Image width: ' + e.relatedTarget.querySelector('img').clientWidth);
});

jQuery('#carouselDemo').on('slid.bs.carousel', function(e) {

  console.log('SLID event');
  console.log('Image width: ' + e.relatedTarget.querySelector('img').clientWidth);
});
.carousel {
  background-color: grey;
}

https://jsfiddle.net/upsidown/ruo0gt19/


编辑

我没有提及我需要下一张幻灯片图像客户端宽度,即显示宽度,而不是原始图像宽度。下面是一个具有较大图像的示例,使用@gaetanoM提供的答案将其设置为最大宽度为100%。

注意:我不能像在我的用例中那样简单地使用主体或容器元素的宽度,轮播图像全屏显示,最大高度为100%,因此宽度可以并小于容器宽度。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<div id="carouselDemo" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="https://via.placeholder.com/700x400">
    </div>
    <div class="carousel-item">
      <img src="https://via.placeholder.com/800x400">
    </div>
    <div class="carousel-item">
      <img src="https://via.placeholder.com/900x400">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselDemo" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselDemo" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
jQuery('#carouselDemo').on('slide.bs.carousel', function(e) {

    var fromelement = jQuery('#carouselDemo .carousel-inner img').eq(e.from);
    var toelement = jQuery('#carouselDemo .carousel-inner img').eq(e.to);

    console.log('slide: Current element Width is: ' + fromelement[0].width);
    console.log('slide: Next element Width is: ' + toelement[0].width);

});

jQuery('#carouselDemo').on('slid.bs.carousel', function(e) {

    var fromelement = jQuery('#carouselDemo .carousel-inner img').eq(e.from);
    var toelement = jQuery('#carouselDemo .carousel-inner img').eq(e.to);

    console.log('slid: Current element Width is: ' + fromelement[0].width);
    console.log('slid: Next element Width is: ' + toelement[0].width);
});
.carousel {
    background-color: grey;
}

.carousel-item > img {
  max-width: 100%;
}

2 个答案:

答案 0 :(得分:1)

根据更新后的答案,问题在滑动时位于next / prev元素上(它是隐藏的,因此没有clientWidth)。

为了克服这个问题,可以采用以下解决方案:

  • 切换此隐藏元素的可见性
  • 获取clientWidth
  • 立即重新切换可见性以适应所有情况。

在摘要中,每张图像的宽度不同以证明结果。

jQuery('#carouselDemo').on('slide.bs.carousel', function(e) {
    var fromelement = jQuery('#carouselDemo .carousel-inner img').eq(e.from);
    var toelement = jQuery('#carouselDemo .carousel-inner img').eq(e.to);

    toelement.parent().toggleClass('active');
    console.log('slide: Current element Width is: ' + fromelement[0].width + ' / ' + fromelement[0].clientWidth);
    console.log('slide: Next element Width is: ' + toelement[0].width + ' / ' + toelement[0].clientWidth);
    toelement.parent().toggleClass('active');
});
jQuery('#carouselDemo').on('slid.bs.carousel', function(e) {
    var fromelement = jQuery('#carouselDemo .carousel-inner img').eq(e.from);
    var toelement = jQuery('#carouselDemo .carousel-inner img').eq(e.to);

    fromelement.parent().toggleClass('active');
    console.log('slid: Current element Width is: ' + fromelement[0].width + ' / ' + fromelement[0].clientWidth);
    console.log('slid: Next element Width is: ' + toelement[0].width + ' / ' + toelement[0].clientWidth);
    fromelement.parent().toggleClass('active');
});
.carousel {
    background-color: grey;
}

.carousel-item > img.n1 {
    width: 100%;
    height: 100vh;
}
.carousel-item > img.n2 {
    width: 50%;
    height: 100vh;
}
.carousel-item > img.n3 {
    width: 75%;
    height: 100vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.js"></script>


<div id="carouselDemo" class="carousel slide" data-ride="carousel">
    <div class="carousel-inner">
        <div class="carousel-item active">
            <img class="n1" src="https://via.placeholder.com/700x400">
        </div>
        <div class="carousel-item">
            <img class="n2"  src="https://via.placeholder.com/800x400">
        </div>
        <div class="carousel-item">
            <img class="n3"  src="https://via.placeholder.com/900x400">
        </div>
    </div>
    <a class="carousel-control-prev" href="#carouselDemo" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="#carouselDemo" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>

阅读How to get image size (height & width) using JavaScript?后,我建议使用普通的HTML img widthnaturalWidth

jQuery('#carouselDemo').on('slide.bs.carousel', function(e) {

    var fromelement = jQuery('#carouselDemo .carousel-inner img').eq(e.from);
    var toelement = jQuery('#carouselDemo .carousel-inner img').eq(e.to);

    console.log('slide: Current element Width is: ' + fromelement[0].width);
    console.log('slide: Next element Width is: ' + toelement[0].width);

});

jQuery('#carouselDemo').on('slid.bs.carousel', function(e) {

    var fromelement = jQuery('#carouselDemo .carousel-inner img').eq(e.from);
    var toelement = jQuery('#carouselDemo .carousel-inner img').eq(e.to);

    console.log('slid: Current element Width is: ' + fromelement[0].width);
    console.log('slid: Next element Width is: ' + toelement[0].width);
});
.carousel {
    background-color: grey;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>


<div id="carouselDemo" class="carousel slide" data-ride="carousel">
    <div class="carousel-inner">
        <div class="carousel-item active">
            <img src="https://via.placeholder.com/700x400">
        </div>
        <div class="carousel-item">
            <img src="https://via.placeholder.com/800x400">
        </div>
        <div class="carousel-item">
            <img src="https://via.placeholder.com/900x400">
        </div>
    </div>
    <a class="carousel-control-prev" href="#carouselDemo" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="#carouselDemo" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>

答案 1 :(得分:-1)

这是因为每个人都在不同的地点开火。 https://getbootstrap.com/docs/4.0/components/carousel/

slide.bs.carousel = This event fires immediately when the slide instance method is invoked.

slid.bs.carousel = This event is fired when the carousel has completed its slide transition.

因此,当您需要下一个图像大小时,滑动会起作用,因为已加载该图像,而滑动事件会在加载下一个图像之前触发。如果由于某种原因必须使用slide事件vs slid,则必须在事件完成后设置某种回调直到事件结束。