2

I have this carousel

$('#myCarousel').carousel({
  interval: 4000
})

$('.carousel .item').each(function(){
  var next = $(this).next();
  if (!next.length) {
    next = $(this).siblings(':first');
  }
  next.children(':first-child').clone().appendTo($(this));

  for (var i=0;i<2;i++) {
    next=next.next();
    if (!next.length) {
      next = $(this).siblings(':first');
    }

    next.children(':first-child').clone().appendTo($(this));
  }
});
.carousel-inner .active.left { left: -25%; }
.carousel-inner .next        { left:  25%; }
.carousel-inner .prev   { left: -25%; }
.carousel-control     { width:  4%; }
.carousel-control.left,.carousel-control.right {margin-left:15px;background-image:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="col-md-12 text-center"><h3>Bootstrap 3 Multiple Slide Carousel</h3></div>
<div class="col-md-6 col-md-offset-3">
  <div class="carousel slide" id="myCarousel">
    <div class="carousel-inner">
      <div class="item active">
        <div class="col-xs-3"><a href="#"><img src="http://placehold.it/500/e499e4/fff&amp;text=1" class="img-responsive"></a></div>
      </div>
      <div class="item">
        <div class="col-xs-3"><a href="#"><img src="http://placehold.it/500/e477e4/fff&amp;text=2" class="img-responsive"></a></div>
      </div>
      <div class="item">
        <div class="col-xs-3"><a href="#"><img src="http://placehold.it/500/eeeeee&amp;text=3" class="img-responsive"></a></div>
      </div>
      <div class="item">
        <div class="col-xs-3"><a href="#"><img src="http://placehold.it/500/f4f4f4&amp;text=4" class="img-responsive"></a></div>
      </div>
      <div class="item">
        <div class="col-xs-3"><a href="#"><img src="http://placehold.it/500/f566f5/333&amp;text=5" class="img-responsive"></a></div>
      </div>
      <div class="item">
        <div class="col-xs-3"><a href="#"><img src="http://placehold.it/500/f477f4/fff&amp;text=6" class="img-responsive"></a></div>
      </div>
      <div class="item">
        <div class="col-xs-3"><a href="#"><img src="http://placehold.it/500/eeeeee&amp;text=7" class="img-responsive"></a></div>
      </div>
      <div class="item">
        <div class="col-xs-3"><a href="#"><img src="http://placehold.it/500/fcfcfc/333&amp;text=8" class="img-responsive"></a></div>
      </div>
    </div>
    <a class="left carousel-control" href="#myCarousel" data-slide="prev"><i class="glyphicon glyphicon-chevron-left"></i></a>
    <a class="right carousel-control" href="#myCarousel" data-slide="next"><i class="glyphicon glyphicon-chevron-right"></i></a>
  </div>
</div>

https://jsfiddle.net/r5twtm22/

this is working fine.

I need to move only single image at once. Its moving all 4 images ..

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
  • how do you mean? you only want to show one image? because if you move one you have to move the rest... – Håkan Jun 06 '16 at 05:22
  • @Håkan actually currently the entire row moves Like if 1,2,3,4 is displayed then next() will first move 1,2,3,4 to left and then new 2,3,4,5 will come up.. right . I need to move like, if 1,2,3,4 is displayed the next() will move 1 out of visible part and add 5 at back. – Atul Sharma Jun 06 '16 at 05:25
  • isn't that the same? – Håkan Jun 06 '16 at 05:32
  • You can see something like yours in this [pen](https://codepen.io/mephysto/pen/ZYVKRY/) – Mosh Feu Jun 06 '16 at 05:34
  • @MoshFeu tried using this also... But, in system or in new fiddle the same thing happens,,, first it moves all 4 then a new row comes with 2,3,4,5 data. What may be the possible reason ? – Atul Sharma Jun 06 '16 at 05:38

2 Answers2

4

For some reason snippet is not showing the exact result. Please check the fiddle

FIDDLE (clean and working version)

$('#myCarousel').carousel({
  interval: 10000
})

$('.carousel .item').each(function(){
  var next = $(this).next();
  if (!next.length) {
    next = $(this).siblings(':first');
  }
  next.children(':first-child').clone().appendTo($(this));
  
  if (next.next().length>0) {
    next.next().children(':first-child').clone().appendTo($(this));
  }
  else {
   $(this).siblings(':first').children(':first-child').clone().appendTo($(this));
  }
});
#myCarousel .carousel-inner .active.left {
  left: -33%;
}
#myCarousel .carousel-inner .active.right {
  left: 33%;
}
#myCarousel .carousel-inner .next {
  left: 33%;
}
#myCarousel .carousel-inner .prev {
  left: -33%;
}
#myCarousel .carousel-control.left {
  background-image: none;
}
#myCarousel .carousel-control.right {
  background-image: none;
}
#myCarousel .carousel-inner .item {
  background: white;
  transition: all ease .5s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div id="myCarousel" class="carousel slide">

  <div class="carousel-inner">
    <div class="item active">
      <div class="col-xs-4">
        <a href="#">
          <img src="http://placehold.it/500/bbbbbb/fff&amp;text=1" class="img-responsive">
        </a>
      </div>
    </div>
    <div class="item">
      <div class="col-xs-4">
        <a href="#">
          <img src="http://placehold.it/500/CCCCCC&amp;text=2" class="img-responsive">
        </a>
      </div>
    </div>
    <div class="item">
      <div class="col-xs-4">
        <a href="#">
          <img src="http://placehold.it/500/eeeeee&amp;text=3" class="img-responsive">
        </a>
      </div>
    </div>
    <div class="item">
      <div class="col-xs-4">
        <a href="#">
          <img src="http://placehold.it/500/f4f4f4&amp;text=4" class="img-responsive">
        </a>
      </div>
    </div>
    <div class="item">
      <div class="col-xs-4">
        <a href="#">
          <img src="http://placehold.it/500/fcfcfc/333&amp;text=5" class="img-responsive">
        </a>
      </div>
    </div>
    <div class="item">
      <div class="col-xs-4">
        <a href="#">
          <img src="http://placehold.it/500/f477f4/fff&amp;text=6" class="img-responsive">
        </a>
      </div>
    </div>
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
Jinu Kurian
  • 9,128
  • 3
  • 27
  • 35
  • I really don't think that it the result he expected. Why do you need to slide all the elements? he need to move just one. – Mosh Feu Jun 06 '16 at 05:48
  • Check the fiddle, its works fine. I don't know why this strange thing happening in this snippet. :/ – Jinu Kurian Jun 06 '16 at 05:49
  • It creates blur effect to text in "item" when is scrolling. Is there any work around to avoid text getting that blur? I have solved it partially by adding white background to "item" and that way is not transparent so you cannot see second row sliding behind. Any better idea? – StefaDesign Nov 02 '17 at 01:32
  • 1
    It's not working here because of the Bootstrap version 3.3.7. If you use Bootstrap 3.0.0 CSS then it will work perfectly. – iamsujit Aug 29 '18 at 13:13
4

Working Fiddle

I was having the same problem, and I got it to only slide by one item at a time by adding this code to the css:

#myCarousel .carousel-inner .item {
  background: white;
  /*transition: transform;*/
  transition: all 500ms ease-out; /* transition is added here */
}

Try this: JS:

$('#myCarousel').carousel({
  interval: 4000
})

$('.carousel .item').each(function(){
  var next = $(this).next();
  if (!next.length) {
    next = $(this).siblings(':first');
  }
  next.children(':first-child').clone().appendTo($(this));

  for (var i=0;i<2;i++) {
    next=next.next();
    if (!next.length) {
      next = $(this).siblings(':first');
    }

    next.children(':first-child').clone().appendTo($(this));
  }
});

CSS:

.carousel-inner .active.left { left: -25%; }
.carousel-inner .next        { left:  25%; }
.carousel-inner .prev        { left: -25%; }
.carousel-control            { width:  4%; }
.carousel-control.left,.carousel-control.right {margin-left:15px;background-image:none;}

#myCarousel .carousel-inner .item {
  background: white;
  /*transition: transform;*/
  transition: all 500ms ease-out; /* transition is added here */
}

HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="bootstrapCarousel.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- <script type="text/javascript" src="http://twitter.github.io/bootstrap/assets/js/bootstrap-transition.js"></script>
    <script type="text/javascript" src="http://twitter.github.io/bootstrap/assets/js/bootstrap-carousel.js"></script> -->
  </head>
  <body>
  <div class="col-md-12 text-center"><h3>Product Carousel</h3></div>

<div class="col-md-6 col-md-offset-3">
<div class="carousel slide row" data-ride="carousel" data-type="multi" data-interval="3000" id="myCarousel">
  <div class="carousel-inner">
    <div class="item active">
      <div class="col-md-3 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/e499e4/fff&amp;text=1" class="img-responsive" /></a></div>
    </div>
    <div class="item">
      <div class="col-md-3 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/e477e4/fff&amp;text=2" class="img-responsive" /></a></div>
    </div>
    <div class="item">
      <div class="col-md-3 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/eeeeee&amp;text=3" class="img-responsive" /></a></div>
    </div>
    <div class="item">
      <div class="col-md-3 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/f4f4f4&amp;text=4" class="img-responsive" /></a></div>
    </div>
    <div class="item">
      <div class="col-md-3 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/f566f5/333&amp;text=5" class="img-responsive" /></a></div>
    </div>
    <div class="item">
      <div class="col-md-3 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/f477f4/fff&amp;text=6" class="img-responsive" /></a></div>
    </div>
    <div class="item">
      <div class="col-md-3 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/eeeeee&amp;text=7" class="img-responsive" /></a></div>
    </div>
    <div class="item">
      <div class="col-md-3 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/fcfcfc/333&amp;text=8" class="img-responsive" /></a></div>
    </div>
  </div>
  <a class="left carousel-control" href="#myCarousel" data-slide="prev"><i class="glyphicon glyphicon-chevron-left"></i></a>
  <a class="right carousel-control" href="#myCarousel" data-slide="next"><i class="glyphicon glyphicon-chevron-right"></i></a>
</div>
</div>

<script src="https://code.jquery.com/jquery-2.0.2.min.js" type="text/javascript"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript" src="bootstrapCarousel.js">
</script>
</body>
</html>

You can also make the transition smoother by adding transition: all 500ms ease-out; to your css.

Fiddle here: http://jsfiddle.net/eaae76kx/98/

Kimmiekim
  • 301
  • 2
  • 6
  • 18
  • Did you ever update this with a transition? Would love to see it added to your answer to make it more smooth. – becca Sep 25 '17 at 18:46
  • you can add ``` transition: all 500ms ease-out;``` to ```#myCarousel .carousel-inner .item ``` in css. I've updated my answer. You can also view Fiddle here: http://jsfiddle.net/eaae76kx/98/ – Kimmiekim May 11 '18 at 19:53