A quick reminder on how to make the carousel in bootstrap compatible with touch devices like smartphones and tablets.
Why?
Feed back was that the user was unimpressed with the image slideshow. You have to tap on the left and right symbols...
How?
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:
copyraw
/*! Bootstrap Carousel Swipe jQuery plugin v1.1 | https://github.com/maaaaark/bcSwipe | MIT License */ !function(t){ t.fn.bcSwipe=function(e){ var n={threshold:50}; return e&&t.extend(n,e),this.each( function(){ function e(t){ 1==t.touches.length&&(u=t.touches[0].pageX,c=!0,this.addEventListener("touchmove",o,!1)) } function o(e){ if(c){ var o=e.touches[0].pageX,i=u-o;Math.abs(i)>=n.threshold&&(h(),t(this).carousel(i>0?"next":"prev")) } } function h(){ this.removeEventListener("touchmove",o),u=null,c=!1 } var u,c=!1;"ontouchstart"in document.documentElement&&this.addEventListener("touchstart",e,!1) } ),this } }(jQuery); $('.carousel').bcSwipe({ threshold: 50 });
- /*! Bootstrap Carousel Swipe jQuery plugin v1.1 | https://github.com/maaaaark/bcSwipe | MIT License */
- !function(t){
- t.fn.bcSwipe=function(e){
- var n={threshold:50};
- return e&&t.extend(n,e),this.each(
- function(){
- function e(t){
- 1==t.touches.length&&(u=t.touches[0].pageX,c=!0,this.addEventListener("touchmove",o,!1))
- }
- function o(e){
- if(c){
- var o=e.touches[0].pageX,i=u-o;Math.abs(i)>=n.threshold&&(h(),t(this).carousel(i>0?"next":"prev"))
- }
- }
- function h(){
- this.removeEventListener("touchmove",o),u=null,c=!1
- }
- var u,c=!1;"ontouchstart"in document.documentElement&&this.addEventListener("touchstart",e,!1)
- }
- ),this
- }
- }(jQuery);
- $('.carousel').bcSwipe({ threshold: 50 });
Source(s):
- Stack Overflow: How to make Bootstrap carousel slider use mobile left/right swipe
- Mark Shiraldi - Github bcSwipe
Category: Bootstrap :: Article: 645