gpt4 book ai didi

javascript - Bootstrap 4 Carousel 获取图片大小

转载 作者:可可西里 更新时间:2023-11-01 14:58:59 25 4
gpt4 key购买 nike

这是一个非常基本的 Bootstrap 4 轮播,包含 3 张图片。

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

我能够在 slid 事件监听器中获取图像宽度(当轮播完成其幻灯片转换时),但是当我尝试时图像宽度始终为 0slide 监听器中获取它(我想知道下一张幻灯片的大小)。

这是为什么?

这是一个片段。观看控制台。

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;
}
<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>

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


编辑

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

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

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%;
}
<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>

最佳答案

根据更新后的答案,问题出在滑动时的下一个/上一个元素上(它是隐藏的,因此没有 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>

关于javascript - Bootstrap 4 Carousel 获取图片大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57417371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com