Monday 17 November 2014

Adding swipe support to Bootstrap Carousel 3.0

With the release of bootstrap version 3, I thought it would be a good time to review my previous post on adding swipe support to bootstraps carousel and verify that everything still works as expected. After a quick wire up with a custom jQuery mobile and downloading all of the new bootstrap components I was pleased to see that nothing from my previous example was broken.

Things have changed a bit however in the year since my previous post. Newer libraries are available, different plugins, the carousel itself has some additional features such as indicators. Instead of simply updating my previous post with the new info, I thought I might delve into some of the more common questions that were asked in my previous posts and show how to do the wireups in a few of the other common touch libraries.

Without any further ado here are some other examples of adding swipe support to bootstraps carousel.
Adding swipe support with jQuery Mobile.

Unless you need the entire library I recommend that you build a custom version availablehere select the “touch” checkbox under events, the current minified version of that for version 1.3.2 is below (jquery.mobile.custom.min.js)

Include this as a separate file in your document source and then add the following script to enable touch support within carousel (note that #myCarousel should match the ID or class of the carousel you’re attempting to bind to).

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

Adding with TouchSwipe plugin
If you don’t want to use jQuery Mobile or are having issues with it, TouchSwipe is another jQuery option for you TouchSwipe Plugin download this and include it in your project. To wire up support for carousel add the following script. *Thanks to Eric for the slide indicator fix here.*


<script> $(document).ready(function() { //Enable swiping... $(".carousel-inner").swipe( { //Generic swipe handler for all directions swipeLeft:function(event, direction, distance, duration, fingerCount) { $(this).parent().carousel('prev'); }, swipeRight: function() { $(this).parent().carousel('next'); }, //Default is 75px, set to 0 for demo so any distance triggers swipe threshold:0 }); }); </script>


Adding support with Hammer.js

Hammer.js is designed to be a straight Javascript alternative, however Bootstrap’s carousel uses jQuery so you’ll be loading that library anyway. Hammer does work with jQuery through. Grab a copy of it Hammer.js then add the following script to your page.


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


Which one should I use?

That’s easy, use the one that fits your project requirements the best. Seriously. Each and every one of these libraries is under 5k minified and gzipped (unless you’re going with the full jQuery.Mobile library). Choose the one that’s the easiest for you to work with.


How do I add swipe support for more than one carousel on a page?
Adding more swipe support for more than one slider is very simple. Change your script to include additional targets. The script below is for jQuery mobile just modify it to fit the library you need.

<script> $(document).ready(function() { //add your other targets here $("#myCarousel, #myOtherCarousel").swiperight(function() { $(this).carousel('prev'); }); //add your other targets here $("#myCarousel, #myOtherCarousel").swipeleft(function() { $(this).carousel('next'); }); }); </script>



3 comments:

  1. You stole this article from : http://lazcreative.com/blog/adding-swipe-support-to-bootstrap-carousel-3-0/

    ReplyDelete
  2. It’s hard to search out educated people on this topic, but you sound like you realize what you’re talking about! Thanks PSD to Wordpress

    ReplyDelete