From 20261b800b9390dd81dce9ef4a1ee6b3d11bfae8 Mon Sep 17 00:00:00 2001 From: Philipp Jenni Date: Sat, 10 Jun 2017 18:25:27 +0200 Subject: [PATCH] Fixed bug: If the carousel does not (yet) contain any items, the swipe event throws an error. Even if you add items later on, the swipe doesn't work anymore. --- jquery.bcSwipe.js | 7 ++++++- jquery.bcSwipe.min.js | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/jquery.bcSwipe.js b/jquery.bcSwipe.js index 216a1e1..b29a1b4 100644 --- a/jquery.bcSwipe.js +++ b/jquery.bcSwipe.js @@ -35,6 +35,7 @@ var difference = start - x; if (Math.abs(difference) >= config.threshold) { cancelTouch(); + if(isCarouselEmpty($(this))) return; if (difference > 0) { $(this).carousel('next'); } @@ -44,6 +45,10 @@ } } } + + function isCarouselEmpty(carousel){ + return carousel.find(".item.active").length < 1 + } function cancelTouch() { this.removeEventListener('touchmove', onTouchMove); @@ -54,4 +59,4 @@ return this; }; -})(jQuery); +})(jQuery); \ No newline at end of file diff --git a/jquery.bcSwipe.min.js b/jquery.bcSwipe.min.js index e8ef802..8691e86 100644 --- a/jquery.bcSwipe.min.js +++ b/jquery.bcSwipe.min.js @@ -1,2 +1,2 @@ /*! 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); +!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",i,!1))}function i(e){if(c){var i=e.touches[0].pageX,r=u-i;if(Math.abs(r)>=n.threshold){if(h(),o(t(this)))return;r>0?t(this).carousel("next"):t(this).carousel("prev")}}}function o(t){return t.find(".item.active").length<1}function h(){this.removeEventListener("touchmove",i),u=null,c=!1}var u,c=!1;"ontouchstart"in document.documentElement&&this.addEventListener("touchstart",e,!1)}),this}}(jQuery);