How to add gap between images in carousel slider or remove glitch when slide

帅比萌擦擦* 提交于 2021-02-05 08:43:05

问题


I made a carousel with reference of this link and originally posted on this link but unfortunately, I want no padding in first image. So I removed padding of first image but that create big width of image.

So I removed padding in all images and for gap I added margin-left

.carousel-item img {
    padding: 0;
    margin-right: 1rem; /* for gap */
}

but that created some type of glitch when I slide it.

Is there a solution for adding gap between images (not in first and last image)?

Codepen Link: 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>

I don't want to use any third-party JS library like Slick.js.


回答1:


I think the biggest problem with such approach is the hard-coded "33.333%" of transition that does not take the gap/margin into account. If you use calc(x% + gap) as the value for the transition, it works well.

Additionally, to accommodate all bootstrap breakpoints that change the amount of visible slides, you need to add different transition offsets (e.g. 20% offset for small screens and 50% offset for big screens). A rough example of such approach can be found in the following codepen.




回答2:


This glitch is due to the fact that the gaps are transparent and you see the next/previous sliding item through them. One idea is to make those gap opaque.

You can for example add a white box-shadow to your image to cover the transparent gap:

.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>


来源:https://stackoverflow.com/questions/55563369/how-to-add-gap-between-images-in-carousel-slider-or-remove-glitch-when-slide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!