From 0c66b739566234597eb04278479551c387ff9eed Mon Sep 17 00:00:00 2001 From: Damian Krzeminski Date: Tue, 15 Oct 2013 12:35:15 -0400 Subject: [PATCH] smarter .next and .prev Both Swipe.next and Swipe.prev take an optional `cycle` parameter. If `cycle` is truthy we rewind slides at the beginning and end. Swipe.cycle still works in the same way, but it's reimplemented as next(true) --- Readme.md | 12 ++++++++---- examples/grumpy.html | 2 ++ index.js | 17 +++++++++-------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Readme.md b/Readme.md index e3ddc44..65c2dce 100644 --- a/Readme.md +++ b/Readme.md @@ -70,13 +70,17 @@ Is on the last visisble item. -### .prev() +### .prev(cycle) - Show the previous item if present, or do nothing. + Show the previous item. + By default, do nothing if showing the first item. + If `cycle` is true and we are on the first item, display the last item. -### .next() +### .next(cycle) - Show the next item if present, or do nothing. + Show the next item. + By default, do nothing if showing the last item. + If `cycle` is true and we are on the last item, display the first item. ### .show(i, [ms], [options]) diff --git a/examples/grumpy.html b/examples/grumpy.html index b54527c..88370a2 100644 --- a/examples/grumpy.html +++ b/examples/grumpy.html @@ -74,6 +74,8 @@ + + diff --git a/index.js b/index.js index d30e528..a8bbcfd 100644 --- a/index.js +++ b/index.js @@ -316,12 +316,7 @@ Swipe.prototype.stop = function(){ */ Swipe.prototype.cycle = function(){ - if (this.isLast()) { - this.currentVisible = -1; - this.next(); - } else { - this.next(); - } + return this.next(true); }; /** @@ -353,7 +348,10 @@ Swipe.prototype.isLast = function(){ * @api public */ -Swipe.prototype.prev = function(){ +Swipe.prototype.prev = function(cycle){ + if (cycle && this.isFirst()) { + this.currentVisible = this.visible; + } this.show(this.currentVisible - 1); return this; }; @@ -365,7 +363,10 @@ Swipe.prototype.prev = function(){ * @api public */ -Swipe.prototype.next = function(){ +Swipe.prototype.next = function(cycle){ + if (cycle && this.isLast()) { + this.currentVisible = -1; + } this.show(this.currentVisible + 1); return this; };