Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add freeMode option #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Swipe can take an optional second parameter – an object of key/value settings:
| **stopPropagation** | Boolean | false | stop event propagation. |
| **draggable** | Boolean | false | listen to mouse events in addition to the touch events |
| **ignore** | String | null | ignore touch events on any element matching this selector |
| **freeMode** | Boolean | true | let slide through multiple slides |
| **callback** | Function | null | 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). |
| **transitionEnd** | Function | null | 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). |

Expand Down
2 changes: 1 addition & 1 deletion build/swipe.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 24 additions & 3 deletions swipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
var index = parseInt(options.startSlide, 10) || 0;
var speed = options.speed || 300;
options.continuous = options.continuous !== undefined ? options.continuous : true;
options.freeMode = options.freeMode !== undefined ? options.freeMode : true;

// check text direction
var slideDir = (function(el, prop, dir) {
Expand Down Expand Up @@ -288,9 +289,19 @@
// increase resistance if first or last slide
if (options.continuous) { // we don't add resistance at the end

translate(circle(index-1), delta.x + slidePos[circle(index-1)], 0);
translate(index, delta.x + slidePos[index], 0);
translate(circle(index+1), delta.x + slidePos[circle(index+1)], 0);
var previousSlideDist = delta.x + slidePos[circle(index-1)];
var currentSlideDist = delta.x + slidePos[index];
var nextSlideDist = delta.x + slidePos[circle(index+1)];

if (!options.freeMode) {
previousSlideDist = Math.min(previousSlideDist, 0);
currentSlideDist = Math.max(Math.min(currentSlideDist, width), -width);
nextSlideDist = Math.max(nextSlideDist, 0);
}

translate(circle(index-1), previousSlideDist, 0);
translate(index, currentSlideDist, 0);
translate(circle(index+1), nextSlideDist, 0);

} else {

Expand All @@ -303,6 +314,16 @@
( Math.abs(delta.x) / width + 1 ) // determine resistance level
: 1 ); // no resistance if false

previousSlideDist = delta.x + slidePos[index-1];
currentSlideDist = delta.x + slidePos[index];
nextSlideDist = delta.x + slidePos[index+1];

if (!options.freeMode) {
previousSlideDist = Math.min(previousSlideDist, 0);
currentSlideDist = Math.max(Math.min(currentSlideDist, width), -width);
nextSlideDist = Math.max(nextSlideDist, 0);
}

// translate 1:1
translate(index-1, delta.x + slidePos[index-1], 0);
translate(index, delta.x + slidePos[index], 0);
Expand Down