如何在圆盘传送带滑块中的图像之间添加间隙或如何在滑动时消除毛刺 [英] How to add gap between images in carousel slider or remove glitch when slide

查看:37
本文介绍了如何在圆盘传送带滑块中的图像之间添加间隙或如何在滑动时消除毛刺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我参考此链接制作了轮播,并最初发布在此链接,但不幸的是,我不希望在第一张图像中填充任何内容.因此,我删除了第一个图像的填充,但创建了较大的图像宽度.

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.

因此我删除了所有图像中的填充,并为空白添加了margin-left

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链接: https://codepen.io/Nisharg/pen/qwajmx

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>

我不想使用任何第三方JS库,例如Slick.js.

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

推荐答案

我认为,这种方法的最大问题是硬编码的"33.333%"过渡没有考虑缺口/边距.如果您将 calc(x%+ gap)用作过渡的值,则效果很好.

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.

此外,要适应所有更改可见幻灯片数量的引导程序断点,您需要添加不同的过渡偏移量(例如,小屏幕偏移量为20%,大屏幕偏移量为50%).可以在下面的 codepen 中找到这种方法的大致示例.

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.

这篇关于如何在圆盘传送带滑块中的图像之间添加间隙或如何在滑动时消除毛刺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆