gpt4 book ai didi

jquery - Bootstrap轮播最后一项将隐藏右侧控件

转载 作者:行者123 更新时间:2023-12-01 00:50:58 24 4
gpt4 key购买 nike

我使用的是bootstrap 4.0版本。我想隐藏第一个项目的左侧控件,并隐藏最后一个项目的右侧控件。我认为使用 jQuery 可以解决这个问题。

左控件将一直隐藏,仅在最后一个项目上显示。右侧控件将一直显示,但隐藏在最后一项中。

HTML:

<div id="carousel-1" class="carousel slide" data-ride="carousel">

<div class="carousel-inner">
<div class="carousel-item active">
Slide 1
</div>
<div class="carousel-item">
Slide 2
</div>
<div class="carousel-item">
Slide 3
</div>
</div>
<a class="carousel-control-prev" href="#carousel-1" 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="#carousel-1" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>

JQuery:

$('.carousel').carousel({
interval: false,
})

function checkitem()
{
var $this = $('#carousel-1');
if ($('.carousel-inner .carousel-item:first').hasClass('active')) {
$this.children('.carousel-control-prev').hide();
} else if ($('.carousel-inner .carousel-item:last').hasClass('active')) {
$this.children('.carousel-control-next').hide();
} else {
$this.children('.carousel-control').show();

}
}

最佳答案

我删除了 data-ride="carousel",因此它不会自动启动,设置了 interval: falsewrap: false .
.d-none ( Bootstrap class ) 添加到 控件 中,以便它们开始隐藏。

如果至少有两个项目,则显示 next 控件。
然后,在每个 slide我们检查下一个位置并相应地显示控件

var carouselLength = $('.carousel-item').length - 1;

// If there is more than one item
if (carouselLength) {
$('.carousel-control-next').removeClass('d-none');
}

$('.carousel').carousel({
interval: false,
wrap: false
}).on('slide.bs.carousel', function (e) {
// First one
if (e.to == 0) {
$('.carousel-control-prev').addClass('d-none');
$('.carousel-control-next').removeClass('d-none');
} // Last one
else if (e.to == carouselLength) {
$('.carousel-control-prev').removeClass('d-none');
$('.carousel-control-next').addClass('d-none');
} // The rest
else {
$('.carousel-control-prev').removeClass('d-none');
$('.carousel-control-next').removeClass('d-none');
}
});
.carousel {
background-color: #ddd;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div id="carousel-1" class="carousel slide">
<div class="carousel-inner">
<div class="carousel-item active">
Slide 1
</div>
<div class="carousel-item">
Slide 2
</div>
<div class="carousel-item">
Slide 3
</div>
</div>
<a class="carousel-control-prev d-none" href="#carousel-1" 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 d-none" href="#carousel-1" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>

关于jquery - Bootstrap轮播最后一项将隐藏右侧控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52921191/

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