Make bootstrap carousel responsive to swipe

A quick reminder on how to make the carousel in bootstrap compatible with touch devices like smartphones and tablets.

Feed back was that the user was unimpressed with the image slideshow. You have to tap on the left and right symbols...

Some will suggest to load the jQueryMobile library but that started messing up the template layouts for me. I really like the solution (and think it should be voted best answer) put forward by Mark Shiraldi:

The code is minimal but does what it needs to do (tested on iOS) and doesn't appear to effect other javascripts. Put this in a JS:
/*! Bootstrap Carousel Swipe jQuery plugin v1.1 | | MIT License */
		var n={threshold:50};
		return e&&t.extend(n,e),this.each(
				function e(t){
				function o(e){
						var o=e.touches[0].pageX,i=u-o;Math.abs(i)>=n.threshold&&(h(),t(this).carousel(i>0?"next":"prev"))
				function h(){
				var u,c=!1;"ontouchstart"in document.documentElement&&this.addEventListener("touchstart",e,!1)
$('.carousel').bcSwipe({ threshold: 50 });
  1.  /*! Bootstrap Carousel Swipe jQuery plugin v1.1 | | MIT License */ 
  2.  !function(t){ 
  3.      t.fn.bcSwipe=function(e){ 
  4.          var n={threshold:50}
  5.          return e&&t.extend(n,e),this.each( 
  6.              function(){ 
  7.                  function e(t){ 
  8.                      1==t.touches.length&&(u=t.touches[0].pageX,c=!0,this.addEventListener("touchmove",o,!1)) 
  9.                  } 
  10.                  function o(e){ 
  11.                      if(c){ 
  12.                          var o=e.touches[0].pageX,i=u-o;Math.abs(i)>=n.threshold&&(h(),t(this).carousel(i>0?"next":"prev")) 
  13.                      } 
  14.                  } 
  15.                  function h(){ 
  16.                      this.removeEventListener("touchmove",o),u=null,c=!1 
  17.                  } 
  18.                  var u,c=!1;"ontouchstart"in document.documentElement&&this.addEventListener("touchstart",e,!1) 
  19.              } 
  20.          ),this 
  21.      } 
  22.  }(jQuery)
  23.  $('.carousel').bcSwipe({ threshold: 50 })

Category: Bootstrap :: Article: 645

Credit where Credit is Due:

Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman

Related Articles

Joes Revolver Map


Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Donate to Joel Lipman via PayPal

Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 bc1qjtp4l4ra452wzvuk9a45yfj82zkahsyy2z379y
© 2023 Joel Lipman .com. All Rights Reserved.