我参考此link制作了一个轮播,并最初发布在此link上,但是不幸的是,我不希望在第一张图像中填充任何内容。所以我删除了第一张图片的填充,但创建了很大的图片宽度。
所以我删除了所有图像的填充,并为空白添加了margin-left
.carousel-item img {
padding: 0;
margin-right: 1rem; /* for gap */
}
但是当我滑动它时会产生某种类型的故障。
因此任何在图像之间添加间隙的解决方案(不在第一张和最后一张图像中)
Codepen链接:https://codepen.io/Nisharg/pen/qwajmx
$('#travelCarousel').carousel({
interval: false
});
$('#travelCarousel.carousel .carousel-item').each(function(){
let next = $(this).next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
for (let i=0;i<3;i++) {
next=next.next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
}
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css">
<style>
.carousel-item img {
padding: 0;
margin-right: 1rem; /* for gap */
}
.carousel-inner .carousel-item.active,
.carousel-inner .carousel-item-next,
.carousel-inner .carousel-item-prev {
display: flex;
}
.carousel-inner .carousel-item-right.active,
.carousel-inner .carousel-item-next {
transform: translateX(33.33333333%);
}
.carousel-inner .carousel-item-left.active,
.carousel-inner .carousel-item-prev {
transform: translateX(-33.33333333%);
}
.carousel-inner .carousel-item-right,
.carousel-inner .carousel-item-left {
transform: translateX(0);
}
</style>
<div class="travel__carousel">
<div id="travelCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="col-md-6 col-xl-4" src="https://picsum.photos/200/300?image=0" alt="img-1">
</div>
<div class="carousel-item">
<img class="col-md-6 col-xl-4" src="https://picsum.photos/200/300?image=2" alt="img-2">
</div>
<div class="carousel-item">
<img class="col-md-6 col-xl-4" src="https://picsum.photos/200/300?image=3" alt="img-3">
</div>
<div class="carousel-item">
<img class="col-md-6 col-xl-4" src="https://picsum.photos/200/300?image=4" alt="img-4">
</div>
<div class="carousel-item">
<img class="col-md-6 col-xl-4" src="https://picsum.photos/200/300?image=5" alt="img-5">
</div>
</div>
</div>
<div class="travel__arrows">
<a class="btn" href="#travelCarousel" role="button" data-slide="prev"><i class="fas fa-arrow-left"></i></a>
<a class="btn" href="#travelCarousel" role="button" data-slide="next"><i class="fas fa-arrow-right"></i></a>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
不要使用像SLICK.JS这样的任何第三方JS
谢谢!
答案 0 :(得分:2)
我认为,这种方法最大的问题是硬编码的“ 33.333%”过渡没有考虑缺口/余量。如果您将calc(x% + gap)
用作过渡的值,则效果很好。
此外,要适应所有更改可见幻灯片数量的引导程序断点,您需要添加不同的过渡偏移量(例如,小屏幕偏移量为20%,大屏幕偏移量为50%)。可以在下面的codepen中找到这种方法的大致示例。
答案 1 :(得分:1)
此故障是由于以下事实造成的:间隙是透明的,并且您可以通过它们看到下一个/上一个滑动项。一种想法是使这些间隙不透明。
例如,您可以在图像中添加白色box-shadow
以覆盖透明间隙:
.carousel-item img {
padding: 0;
margin-right: 1rem;
/*added this*/
box-shadow:
1rem 0 #fff,
-1rem 0 0 #fff;
}
$('#travelCarousel').carousel({
interval: false
});
$('#travelCarousel.carousel .carousel-item').each(function(){
let next = $(this).next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
for (let i=0;i<3;i++) {
next=next.next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
}
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css">
<style>
.carousel-item img {
padding: 0;
margin-right: 1rem; /* for gap */
box-shadow:
1rem 0 0 #fff,
-1rem 0 0 #fff;
}
.carousel-inner .carousel-item.active,
.carousel-inner .carousel-item-next,
.carousel-inner .carousel-item-prev {
display: flex;
}
.carousel-inner .carousel-item-right.active,
.carousel-inner .carousel-item-next {
transform: translateX(33.33333333%);
}
.carousel-inner .carousel-item-left.active,
.carousel-inner .carousel-item-prev {
transform: translateX(-33.33333333%);
}
.carousel-inner .carousel-item-right,
.carousel-inner .carousel-item-left {
transform: translateX(0);
}
</style>
<div class="travel__carousel">
<div id="travelCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="col-md-6 col-xl-4" src="https://picsum.photos/200/300?image=0" alt="img-1">
</div>
<div class="carousel-item">
<img class="col-md-6 col-xl-4" src="https://picsum.photos/200/300?image=2" alt="img-2">
</div>
<div class="carousel-item">
<img class="col-md-6 col-xl-4" src="https://picsum.photos/200/300?image=3" alt="img-3">
</div>
<div class="carousel-item">
<img class="col-md-6 col-xl-4" src="https://picsum.photos/200/300?image=4" alt="img-4">
</div>
<div class="carousel-item">
<img class="col-md-6 col-xl-4" src="https://picsum.photos/200/300?image=5" alt="img-5">
</div>
</div>
</div>
<div class="travel__arrows">
<a class="btn" href="#travelCarousel" role="button" data-slide="prev"><i class="fas fa-arrow-left"></i></a>
<a class="btn" href="#travelCarousel" role="button" data-slide="next"><i class="fas fa-arrow-right"></i></a>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>