75

DEMO JSSFIDDLE

  <div class="col-md-4">
          <!--Carousel-->
          <div id="sidebar-carousel-1" class="carousel slide" data-ride="carousel">
            <ol class="carousel-indicators grey">
              <li data-target="#sidebar-carousel-1" data-slide-to="0" class="active"></li>
              <li data-target="#sidebar-carousel-1" data-slide-to="1"></li>
              <li data-target="#sidebar-carousel-1" data-slide-to="2"></li>
            </ol>
            <div class="carousel-inner">
              <div class="item active">
                <a href="" data-lightbox="image-1" title="">
                <img src="http://shrani.si/f/3D/13b/1rt3lPab/2/img1.jpg" alt="...">
                </a>
              </div>
              <div class="item">
                <a href="" data-lightbox="image-1" title="">
                <img src="http://shrani.si/f/S/jE/UcTuhZ6/1/img2.jpg" alt="...">
                </a>                  </div>
              <div class="item">
                <a href="" data-lightbox="image-1" title="">
                <img src="http://shrani.si/f/h/To/1yjUEZbP/img3.jpg" alt="...">
                </a>                  </div>
            </div>
             <!-- Controls -->
        <a class="left carousel-control" href="#sidebar-carousel-1" data-slide="prev">
          <span class="glyphicon glyphicon-chevron-left"></span>
        </a>
        <a class="right carousel-control" href="#sidebar-carousel-1" data-slide="next">
          <span class="glyphicon glyphicon-chevron-right"></span>
        </a>
                  </div><!--/Carousel--></div>

What I want to do is to add left/right touch swipe functionality for mobile devices only. How can I do that?

Also, how do I make prev/next buttons, that appear on hover, smaller for mobile/ipad versions?

Thanks

2
  • Note: There is no "hover" on most mobile devices.
    – Patric
    Apr 17, 2014 at 9:41
  • You probably should change the top answer for this question
    – Liam
    Apr 16, 2020 at 12:10

10 Answers 10

151

I'm a bit late to the party, but here's a bit of jQuery I've been using:

$('.carousel').on('touchstart', function(event){
    const xClick = event.originalEvent.touches[0].pageX;
    $(this).one('touchmove', function(event){
        const xMove = event.originalEvent.touches[0].pageX;
        const sensitivityInPx = 5;

        if( Math.floor(xClick - xMove) > sensitivityInPx ){
            $(this).carousel('next');
        }
        else if( Math.floor(xClick - xMove) < -sensitivityInPx ){
            $(this).carousel('prev');
        }
    });
    $(this).on('touchend', function(){
        $(this).off('touchmove');
    });
});

No need for jQuery mobile or any other plugins. If you need to adjust the sensitivity of the swipe adjust the 5 and -5.

11
  • 9
    Great solution, although you have the 'prev' and 'next' inverted. Much lighter-weight than adding a full mobile jQuery solution. Thanks. Nov 11, 2016 at 3:14
  • 7
    Wow, cool solution! But I have several carousels on a page. So I prefer to use $(this) instead of $(".carousel') inside the function. And I also have swapped prev and next :) Jan 13, 2017 at 10:56
  • 7
    I amended the answer
    – Liam
    Feb 2, 2017 at 9:39
  • 1
    Finally! I've tried using the various other methods to make swipe work in Demandware. Their new version, SFRA, has bootstrap-inspired roots, but the other fixes that are supposed to work for bootstrap don't work in Demandware. This was the answer! Thanks!
    – Leann
    Jul 30, 2019 at 8:22
  • 1
    bless you and jquery. this worked first time with no extra cdn's etc .
    – Glen
    Apr 30, 2020 at 21:49
111

I needed to add this functionality to a project I was working on recently and adding jQuery Mobile just to solve this problem seemed like overkill, so I came up with a solution and put it on github: bcSwipe (Bootstrap Carousel Swipe).

It's a lightweight jQuery plugin (~600 bytes minified vs jQuery Mobile touch events at 8kb), and it's been tested on Android and iOS.

This is how you use it:

$('.carousel').bcSwipe({ threshold: 50 });
10
  • 1
    OMG!!! THANK YOU!!! YOU SAVED MY LIFE!!! the .carousel approach has issue when sliding but with your code, it works! Apr 1, 2016 at 9:55
  • 3
    @Mark Shiraldi I have tried to use the Bootstrap Carousel Swipe and script and yet it does not work correctly Aug 12, 2016 at 22:33
  • 2
    Good one Maaark. I just tried this using Bootstrap v4.0.0-alpha.5 and jQuery 3.1.1. Swiping is working fine on my Android smartphone and rendering automatically on large screens as expected.
    – Chris O
    Jan 2, 2017 at 5:37
  • Update: I realised this was a mistake in my layout. bcSwipe works great, it's the best solution I've found
    – Daniel
    Mar 29, 2017 at 18:27
  • @ChrisO so this woeks only with bootstrapv4? Jun 29, 2017 at 10:52
101

UPDATE:

I came up with this solution when I was pretty new to web design. Now that I am older and wiser, the answer Liam gave seems to be a better option. See the next top answer; it is a more productive solution.

I worked on a project recently and this example worked perfectly. I am giving you the link below.

First you have to add jQuery mobile:

http://jquerymobile.com/

This adds the touch functionality, and then you just have to make events such as swipe:

<script>
$(document).ready(function() {
   $("#myCarousel").swiperight(function() {
      $(this).carousel('prev');
    });
   $("#myCarousel").swipeleft(function() {
      $(this).carousel('next');
   });
});
</script>

The link is below, where you can find the tutorial I used:

http://lazcreative.com/blog/how-to/how-to-adding-swipe-support-to-bootstraps-carousel/

4
  • 14
    Definitely follow the advice and build a touch event-only custom jQuery Mobile version at jquerymobile.com/download-builder. Otherwise you will likely break your site if it is not already built for/with jQuery Mobile.
    – cdonner
    May 22, 2014 at 23:08
  • 17
    I am voting this down due to the heavy-handedness of installing jQuery mobile to solve only one simple issue. For simplicity I suggest the answer by Mark Shiraldi, it is graceful and less code. Jun 4, 2015 at 15:38
  • 1
    @nicholas.alipaz well, it looks like you're right. Installing completly new grid system for such small feature is not a good solution.
    – David
    Jul 7, 2015 at 7:09
  • 1
    I don't like this solution, because the slide does not "stick" to your finger as you move over it. It's not user friendly.
    – Wouter
    Nov 18, 2015 at 15:43
5

With bootstrap 4.2 its now very easy, you just need to pass the touch option in the carousel div as data-touch="true", as it accepts Boolean value only.

As in your case update bootstrap to 4.2 and paste(Or download source files) the following in exact order :

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

and then add the touch option in your bootstrap carousel div

    <div id="sidebar-carousel-1" class="carousel slide" data-ride="carousel" data-touch="true">
2
  • 1
    hello, I'm using bootstrap 4.5.3 and the touch is native. however it does not work until you touch one of the two arrows to move to the next/previous image; then it starts working, does anyone else has the same problem? thank you in advance :)
    – Camaleo
    Dec 10, 2020 at 17:18
  • @Camaleo same issue with bootstrap 5. If you set data-bs-ride="carousel" then it responds to swipes even before the cycle begins. If you don't want it to cycle you could set data-bs-interval="9000000000" for 100 days between slides. Sep 4, 2022 at 4:04
4

See this solution: Bootstrap TouchCarousel. A drop-in perfection for Twitter Bootstrap's Carousel (v3) to enable gestures on touch devices. http://ixisio.github.io/bootstrap-touch-carousel/

1
  • 3
    I really like this approach but I don't think the package is ready for a production setting yet. The carousel has some large bugs like links/modals not working after carouslel initialization etc.
    – am80l
    Jul 25, 2014 at 17:08
3

If you don't want to use jQuery mobile as like me. You can use Hammer.js

It's mostly like jQuery Mobile without unnecessary code.

$(document).ready(function() {
  Hammer(myCarousel).on("swipeleft", function(){
    $(this).carousel('next');
  });
  Hammer(myCarousel).on("swiperight", function(){
    $(this).carousel('prev');
  });
});
2

Checked solution is not accurate, sometimes mouse-right-click triggers right-swipe. after trying different plugins for swipe i found an almost perfect one.

touchSwipe

i said "almost" because this plugin does not support future elements. so we would have to reinitialize the swipe call when the swipe content is changed by ajax or something. this plugin have lots of options to play with touch events like multi-finger-touch,pinch etc.

http://labs.rampinteractive.co.uk/touchSwipe/demos/index.html

solution 1 :

$("#myCarousel").swipe( {
            swipe:function(event, direction, distance, duration, fingerCount, fingerData) {

                if(direction=='left'){
                    $(this).carousel('next');
                }else if(direction=='right'){
                    $(this).carousel('prev');
                }

            }
        });

solution 2:(for future element case)

function addSwipeTo(selector){
            $(selector).swipe("destroy");
            $(selector).swipe( {
                swipe:function(event, direction, distance, duration, fingerCount, fingerData) {
                    if(direction=='left'){
                        $(this).carousel('next');
                    }else if(direction=='right'){
                        $(this).carousel('prev');
                    }
                }
            });
}
addSwipeTo("#myCarousel");
2

For anyone finding this, swipe on carousel appears to be native as of about 5 days ago (20 Oct 2018) as per
https://github.com/twbs/bootstrap/pull/25776

https://deploy-preview-25776--twbs-bootstrap4.netlify.com/docs/4.1/components/carousel/

4
  • Can you provide some information on how to activate it? I am using 4.1.3 and swiping is not working by default
    – james
    Oct 25, 2018 at 18:19
  • @binnyb It's probably a pretty shady way of doing it, but I just grabbed the .js from that preview page which works with swipe i.e. deploy-preview-25776--twbs-bootstrap4.netlify.com/docs/4.1/dist/… So not quite native but must be close.
    – geo
    Oct 26, 2018 at 0:53
  • Wow, the diff between Production 4.1.3 and your linked version is so different- 462 differences. A little scary when considering using it.
    – james
    Oct 26, 2018 at 12:50
  • I was trying to find out a way to implement a "Swipe" event for the carousel, but yes, the functionnality is there (native) but only works if you are on a mobile device. getbootstrap.com/docs/4.3/components/carousel/#with-controls Jul 9, 2019 at 13:51
1

Same functionality I prefer than using much heavy jQuery mobile is Carousel Swipe. I suggest directly jump in to source code on github, and copy file carousel-swipe.js in to your project directory.

Use swiper events as bellow:

$('#carousel-example-generic').carousel({
      interval: false 
  });
0

If anyone is looking for the angular version of this answer then I would suggest creating a directive would be a great idea.

NOTE: ngx-bootstrap is used.

import { Directive, Host, Self, Optional, Input, Renderer2, OnInit, ElementRef } from '@angular/core';
import { CarouselComponent } from 'ngx-bootstrap/carousel';

@Directive({
  selector: '[appCarouselSwipe]'
})
export class AppCarouselSwipeDirective implements OnInit {
  @Input() swipeThreshold = 50;
  private start: number;
  private stillMoving: boolean;
  private moveListener: Function;

  constructor(
    @Host() @Self() @Optional() private carousel: CarouselComponent,
    private renderer: Renderer2,
    private element: ElementRef
  ) {
  }

  ngOnInit(): void {
    if ('ontouchstart' in document.documentElement) {
      this.renderer.listen(this.element.nativeElement, 'touchstart', this.onTouchStart.bind(this));
      this.renderer.listen(this.element.nativeElement, 'touchend', this.onTouchEnd.bind(this));
    }
  }

  private onTouchStart(e: TouchEvent): void {
    if (e.touches.length === 1) {
      this.start = e.touches[0].pageX;
      this.stillMoving = true;
      this.moveListener = this.renderer.listen(this.element.nativeElement, 'touchmove', this.onTouchMove.bind(this));
    }
  }

  private onTouchMove(e: TouchEvent): void {
    if (this.stillMoving) {
      const x = e.touches[0].pageX;
      const difference = this.start - x;
      if (Math.abs(difference) >= this.swipeThreshold) {
        this.cancelTouch();
        if (difference > 0) {
          if (this.carousel.activeSlide < this.carousel.slides.length - 1) {
            this.carousel.activeSlide = this.carousel.activeSlide + 1;
          }
        } else {
          if (this.carousel.activeSlide > 0) {
            this.carousel.activeSlide = this.carousel.activeSlide - 1;
          }
        }
      }
    }
  }

  private onTouchEnd(e: TouchEvent): void {
    this.cancelTouch();
  }

  private cancelTouch() {
    if (this.moveListener) {
      this.moveListener();
      this.moveListener = undefined;
    }
    this.start = null;
    this.stillMoving = false;
  }
}

in html:

<carousel appCarouselSwipe>

    ...

</carousel>

Reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.