From 96052c98be502fe3a4816b7f3a118600f40dbf68 Mon Sep 17 00:00:00 2001 From: Lasse Date: Thu, 11 May 2017 00:09:21 +0200 Subject: [PATCH] Added boolean to see if the swipe is from a user interaction --- README.md | 4 ++-- build/swipe.min.js | 2 +- swipe.js | 7 ++++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4fad632..670f085 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ Swipe can take an optional second parameter– an object of key/value settings: - **autoRestart** Boolean *(default: `false`)*: auto restart slideshow after user's touch event or next/prev calls. - **disableScroll** Boolean *(default: `false`)*: prevent any touch events on this container from scrolling the page. - **stopPropagation** Boolean *(default: `false`)*: stop event propagation. -- **callback** Function *(default: `function() {}`)*: runs at slide change. Three parameters are passed to the function: `index` (the current slide index), `elem` (the current slide element) and `dir` (direction: `1` for left or backward, `-1` for right or forward). +- **callback** Function *(default: `function() {}`)*: runs at slide change. Four parameters are passed to the function: `index` (the current slide index), `elem` (the current slide element), `dir` (direction: `1` for left or backward, `-1` for right or forward) and `isInteraction` (is the swipe from a user interaction). - **transitionEnd** Function *(default: `function() {}`)*: runs at the end of a slide transition. Two parameters are passed to the function: `index` (the current slide index) and `elem` (the current slide element). ### Example @@ -103,7 +103,7 @@ window.mySwipe = new Swipe(document.getElementById('slider'), { continuous: true, disableScroll: false, stopPropagation: false, - callback: function(index, elem, dir) {}, + callback: function(index, elem, dir, isInteraction) {}, transitionEnd: function(index, elem) {} }); ``` diff --git a/build/swipe.min.js b/build/swipe.min.js index 7825c51..f8967a5 100644 --- a/build/swipe.min.js +++ b/build/swipe.min.js @@ -5,4 +5,4 @@ * Copyright 2013, MIT License * */ -!function(a,b){"function"==typeof define&&define.amd?define([],function(){return a.Swipe=b(),a.Swipe}):"object"==typeof module&&module.exports?module.exports=b():a.Swipe=b()}(this,function(){function Swipe(c,d){"use strict";function e(){E.addEventListener?(J.removeEventListener("touchstart",N,!1),J.removeEventListener("mousedown",N,!1),J.removeEventListener("webkitTransitionEnd",N,!1),J.removeEventListener("msTransitionEnd",N,!1),J.removeEventListener("oTransitionEnd",N,!1),J.removeEventListener("otransitionend",N,!1),J.removeEventListener("transitionend",N,!1),a.removeEventListener("resize",N,!1)):a.onresize=null}function f(){E.addEventListener?(E.touch&&J.addEventListener("touchstart",N,!1),d.draggable&&J.addEventListener("mousedown",N,!1),E.transitions&&(J.addEventListener("webkitTransitionEnd",N,!1),J.addEventListener("msTransitionEnd",N,!1),J.addEventListener("oTransitionEnd",N,!1),J.addEventListener("otransitionend",N,!1),J.addEventListener("transitionend",N,!1)),a.addEventListener("resize",N,!1)):a.onresize=M}function g(){F=J.children,I=F.length;for(var a=0;ah?-H:K=I&&(a-=I),a}function n(a,b){if(a="number"!=typeof a?parseInt(a,10):a,K!==a){if(E.transitions){var c=Math.abs(K-a)/(K-a);if(d.continuous){var e=c;c=-G[l(a)]/H,c!==e&&(a=-c*F.length+a)}for(var f=Math.abs(K-a)-1;f--;)o(l((a>K?a:K)-f-1),H*c,0);a=l(a),o(K,H*c,b||L),o(a,0,b||L),d.continuous&&o(l(a-c),-(H*c),0)}else a=l(a),q(K*-H,a*-H,b||L);K=a,C(function(){j(m(),F[K],c)})}}function o(a,b,c){p(a,b,c),G[a]=b}function p(a,b,c){var d=F[a],e=d&&d.style;e&&(e.webkitTransitionDuration=e.MozTransitionDuration=e.msTransitionDuration=e.OTransitionDuration=e.transitionDuration=c+"ms",e.webkitTransform="translate("+b+"px,0)translateZ(0)",e.msTransform=e.MozTransform=e.OTransform="translateX("+b+"px)")}function q(a,b,c){if(!c)return void(J.style.left=b+"px");var e=+new Date,f=setInterval(function(){var g=+new Date-e;return g>c?(J.style.left=b+"px",(A||d.autoRestart)&&t(),k(m(),F[K]),void clearInterval(f)):void(J.style.left=(b-a)*(Math.floor(g/c*100)/100)+a+"px")},4)}function r(){x=setTimeout(i,A)}function s(){A=0,clearTimeout(x)}function t(){s(),A=d.auto||0,r()}function u(a){return/^mouse/.test(a.type)}function v(){s(),c.style.visibility="",J.style.width="",J.style.left="";for(var a=F.length;a--;){E.transitions&&p(a,0,0);var b=F[a];if(b.getAttribute("data-cloned")){var d=b.parentElement;d.removeChild(b)}b.style.width="",b.style.left="",b.style.webkitTransitionDuration=b.style.MozTransitionDuration=b.style.msTransitionDuration=b.style.OTransitionDuration=b.style.transitionDuration="",b.style.webkitTransform=b.style.msTransform=b.style.MozTransform=b.style.OTransform=""}e(),M.cancel()}d=d||{};var w,x,y={},z={},A=d.auto||0,B=function(){},C=function(a){setTimeout(a||B,0)},D=function(a,b){function c(){e&&clearTimeout(e)}function d(){var d=this,f=arguments;c(),e=setTimeout(function(){e=null,a.apply(d,f)},b)}b=b||100;var e=null;return d.cancel=c,d},E={addEventListener:!!a.addEventListener,touch:"ontouchstart"in a||a.DocumentTouch&&b instanceof DocumentTouch,transitions:function(a){var b=["transitionProperty","WebkitTransition","MozTransition","OTransition","msTransition"];for(var c in b)if(void 0!==a.style[b[c]])return!0;return!1}(b.createElement("swipe"))};if(c){var F,G,H,I,J=c.children[0],K=parseInt(d.startSlide,10)||0,L=d.speed||300;d.continuous=void 0===d.continuous||d.continuous,d.autoRestart=void 0!==d.autoRestart&&d.autoRestart;var M=D(g),N={handleEvent:function(a){switch(a.type){case"mousedown":case"touchstart":this.start(a);break;case"mousemove":case"touchmove":this.move(a);break;case"mouseup":case"mouseleave":case"touchend":this.end(a);break;case"webkitTransitionEnd":case"msTransitionEnd":case"oTransitionEnd":case"otransitionend":case"transitionend":this.transitionEnd(a);break;case"resize":M()}d.stopPropagation&&a.stopPropagation()},start:function(a){var b;u(a)?(b=a,a.preventDefault()):b=a.touches[0],y={x:b.pageX,y:b.pageY,time:+new Date},w=void 0,z={},u(a)?(J.addEventListener("mousemove",this,!1),J.addEventListener("mouseup",this,!1),J.addEventListener("mouseleave",this,!1)):(J.addEventListener("touchmove",this,!1),J.addEventListener("touchend",this,!1))},move:function(a){var b;if(u(a))b=a;else{if(a.touches.length>1||a.scale&&1!==a.scale)return;d.disableScroll&&a.preventDefault(),b=a.touches[0]}z={x:b.pageX-y.x,y:b.pageY-y.y},"undefined"==typeof w&&(w=!!(w||Math.abs(z.x)0||K===F.length-1&&z.x<0?Math.abs(z.x)/H+1:1),p(K-1,z.x+G[K-1],0),p(K,z.x+G[K],0),p(K+1,z.x+G[K+1],0)))},end:function(a){var b=+new Date-y.time,c=Number(b)<250&&Math.abs(z.x)>20||Math.abs(z.x)>H/2,e=!K&&z.x>0||K===F.length-1&&z.x<0;d.continuous&&(e=!1);var f=Math.abs(z.x)/z.x;w||(c&&!e?(f<0?(d.continuous?(o(l(K-1),-H,0),o(l(K+2),H,0)):o(K-1,-H,0),o(K,G[K]-H,L),o(l(K+1),G[l(K+1)]-H,L),K=l(K+1)):(d.continuous?(o(l(K+1),H,0),o(l(K-2),-H,0)):o(K+1,H,0),o(K,G[K]+H,L),o(l(K-1),G[l(K-1)]+H,L),K=l(K-1)),j(m(),F[K],f)):d.continuous?(o(l(K-1),-H,L),o(K,0,L),o(l(K+1),H,L)):(o(K-1,-H,L),o(K,0,L),o(K+1,H,L))),u(a)?(J.removeEventListener("mousemove",N,!1),J.removeEventListener("mouseup",N,!1),J.removeEventListener("mouseleave",N,!1)):(J.removeEventListener("touchmove",N,!1),J.removeEventListener("touchend",N,!1))},transitionEnd:function(a){var b=parseInt(a.target.getAttribute("data-index"),10);b===K&&((A||d.autoRestart)&&t(),k(m(),F[K]))}};return g(),A&&r(),{setup:g,slide:function(a,b){s(),n(a,b)},prev:function(){s(),h()},next:function(){s(),i()},restart:t,stop:s,getPos:m,getNumSlides:function(){return I},kill:v}}}var a="object"==typeof self&&self.self===self&&self||"object"==typeof global&&global.global===global&&global||this,b=a.document;return(a.jQuery||a.Zepto)&&!function(a){a.fn.Swipe=function(b){return this.each(function(){a(this).data("Swipe",new Swipe(a(this)[0],b))})}}(a.jQuery||a.Zepto),Swipe}); \ No newline at end of file +!function(a,b){"function"==typeof define&&define.amd?define([],function(){return a.Swipe=b(),a.Swipe}):"object"==typeof module&&module.exports?module.exports=b():a.Swipe=b()}(this,function(){function Swipe(c,d){"use strict";function e(){E.addEventListener?(J.removeEventListener("touchstart",N,!1),J.removeEventListener("mousedown",N,!1),J.removeEventListener("webkitTransitionEnd",N,!1),J.removeEventListener("msTransitionEnd",N,!1),J.removeEventListener("oTransitionEnd",N,!1),J.removeEventListener("otransitionend",N,!1),J.removeEventListener("transitionend",N,!1),a.removeEventListener("resize",N,!1)):a.onresize=null}function f(){E.addEventListener?(E.touch&&J.addEventListener("touchstart",N,!1),d.draggable&&J.addEventListener("mousedown",N,!1),E.transitions&&(J.addEventListener("webkitTransitionEnd",N,!1),J.addEventListener("msTransitionEnd",N,!1),J.addEventListener("oTransitionEnd",N,!1),J.addEventListener("otransitionend",N,!1),J.addEventListener("transitionend",N,!1)),a.addEventListener("resize",N,!1)):a.onresize=M}function g(){F=J.children,I=F.length;for(var a=0;ah?-H:K=I&&(a-=I),a}function n(a,b){if(a="number"!=typeof a?parseInt(a,10):a,K!==a){if(E.transitions){var c=Math.abs(K-a)/(K-a);if(d.continuous){var e=c;c=-G[l(a)]/H,c!==e&&(a=-c*F.length+a)}for(var f=Math.abs(K-a)-1;f--;)o(l((a>K?a:K)-f-1),H*c,0);a=l(a),o(K,H*c,b||L),o(a,0,b||L),d.continuous&&o(l(a-c),-H*c,0)}else a=l(a),q(K*-H,a*-H,b||L);K=a,C(function(){j(m(),F[K],c)})}}function o(a,b,c){p(a,b,c),G[a]=b}function p(a,b,c){var d=F[a],e=d&&d.style;e&&(e.webkitTransitionDuration=e.MozTransitionDuration=e.msTransitionDuration=e.OTransitionDuration=e.transitionDuration=c+"ms",e.webkitTransform="translate("+b+"px,0)translateZ(0)",e.msTransform=e.MozTransform=e.OTransform="translateX("+b+"px)")}function q(a,b,c){if(!c)return void(J.style.left=b+"px");var e=+new Date,f=setInterval(function(){var g=+new Date-e;if(g>c)return J.style.left=b+"px",(A||d.autoRestart)&&t(),k(m(),F[K]),void clearInterval(f);J.style.left=(b-a)*(Math.floor(g/c*100)/100)+a+"px"},4)}function r(){x=setTimeout(i,A)}function s(){A=0,clearTimeout(x)}function t(){s(),A=d.auto||0,r()}function u(a){return/^mouse/.test(a.type)}function v(){s(),c.style.visibility="",J.style.width="",J.style.left="";for(var a=F.length;a--;){E.transitions&&p(a,0,0);var b=F[a];if(b.getAttribute("data-cloned")){b.parentElement.removeChild(b)}b.style.width="",b.style.left="",b.style.webkitTransitionDuration=b.style.MozTransitionDuration=b.style.msTransitionDuration=b.style.OTransitionDuration=b.style.transitionDuration="",b.style.webkitTransform=b.style.msTransform=b.style.MozTransform=b.style.OTransform=""}e(),M.cancel()}d=d||{};var w,x,y={},z={},A=d.auto||0,B=function(){},C=function(a){setTimeout(a||B,0)},D=function(a,b){function c(){e&&clearTimeout(e)}function d(){var d=this,f=arguments;c(),e=setTimeout(function(){e=null,a.apply(d,f)},b)}b=b||100;var e=null;return d.cancel=c,d},E={addEventListener:!!a.addEventListener,touch:"ontouchstart"in a||a.DocumentTouch&&b instanceof DocumentTouch,transitions:function(a){var b=["transitionProperty","WebkitTransition","MozTransition","OTransition","msTransition"];for(var c in b)if(void 0!==a.style[b[c]])return!0;return!1}(b.createElement("swipe"))};if(c){var F,G,H,I,J=c.children[0],K=parseInt(d.startSlide,10)||0,L=d.speed||300;d.continuous=void 0===d.continuous||d.continuous,d.autoRestart=void 0!==d.autoRestart&&d.autoRestart;var M=D(g),N={handleEvent:function(a){switch(a.type){case"mousedown":case"touchstart":this.start(a);break;case"mousemove":case"touchmove":this.move(a);break;case"mouseup":case"mouseleave":case"touchend":this.end(a);break;case"webkitTransitionEnd":case"msTransitionEnd":case"oTransitionEnd":case"otransitionend":case"transitionend":this.transitionEnd(a);break;case"resize":M()}d.stopPropagation&&a.stopPropagation()},start:function(a){var b;u(a)?(b=a,a.preventDefault()):b=a.touches[0],y={x:b.pageX,y:b.pageY,time:+new Date},w=void 0,z={},u(a)?(J.addEventListener("mousemove",this,!1),J.addEventListener("mouseup",this,!1),J.addEventListener("mouseleave",this,!1)):(J.addEventListener("touchmove",this,!1),J.addEventListener("touchend",this,!1))},move:function(a){var b;if(u(a))b=a;else{if(a.touches.length>1||a.scale&&1!==a.scale)return;d.disableScroll&&a.preventDefault(),b=a.touches[0]}z={x:b.pageX-y.x,y:b.pageY-y.y},void 0===w&&(w=!!(w||Math.abs(z.x)0||K===F.length-1&&z.x<0?Math.abs(z.x)/H+1:1),p(K-1,z.x+G[K-1],0),p(K,z.x+G[K],0),p(K+1,z.x+G[K+1],0)))},end:function(a){var b=+new Date-y.time,c=Number(b)<250&&Math.abs(z.x)>20||Math.abs(z.x)>H/2,e=!K&&z.x>0||K===F.length-1&&z.x<0;d.continuous&&(e=!1);var f=Math.abs(z.x)/z.x;w||(c&&!e?(f<0?(d.continuous?(o(l(K-1),-H,0),o(l(K+2),H,0)):o(K-1,-H,0),o(K,G[K]-H,L),o(l(K+1),G[l(K+1)]-H,L),K=l(K+1)):(d.continuous?(o(l(K+1),H,0),o(l(K-2),-H,0)):o(K+1,H,0),o(K,G[K]+H,L),o(l(K-1),G[l(K-1)]+H,L),K=l(K-1)),j(m(),F[K],f,!0)):d.continuous?(o(l(K-1),-H,L),o(K,0,L),o(l(K+1),H,L)):(o(K-1,-H,L),o(K,0,L),o(K+1,H,L))),u(a)?(J.removeEventListener("mousemove",N,!1),J.removeEventListener("mouseup",N,!1),J.removeEventListener("mouseleave",N,!1)):(J.removeEventListener("touchmove",N,!1),J.removeEventListener("touchend",N,!1))},transitionEnd:function(a){parseInt(a.target.getAttribute("data-index"),10)===K&&((A||d.autoRestart)&&t(),k(m(),F[K]))}};return g(),A&&r(),{setup:g,slide:function(a,b){s(),n(a,b)},prev:function(){s(),h()},next:function(){s(),i()},restart:t,stop:s,getPos:m,getNumSlides:function(){return I},kill:v}}}var a="object"==typeof self&&self.self===self&&self||"object"==typeof global&&global.global===global&&global||this,b=a.document;return(a.jQuery||a.Zepto)&&function(a){a.fn.Swipe=function(b){return this.each(function(){a(this).data("Swipe",new Swipe(a(this)[0],b))})}}(a.jQuery||a.Zepto),Swipe}); \ No newline at end of file diff --git a/swipe.js b/swipe.js index c23eba9..82208fa 100644 --- a/swipe.js +++ b/swipe.js @@ -302,7 +302,7 @@ index = circle(index-1); } - runCallback(getPos(), slides[index], direction); + runCallback(getPos(), slides[index], direction, true); } else { @@ -520,9 +520,10 @@ } } - function runCallback(pos, index, dir) { + function runCallback(pos, index, dir, isInteraction) { + isInteraction = isInteraction ? true : false; if (options.callback) { - options.callback(pos, index, dir); + options.callback(pos, index, dir, isInteraction); } }