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

Implement ‘threshold’ option. #466

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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Swipe can take an optional second parameter– an object of key/value settings:

- **continuous** Boolean *(default:true)* - create an infinite feel with no endpoints

- **threshold** Float *(default:0.5)* - proportion of next frame that must be dragged in before slow swipe will trigger

- **disableScroll** Boolean *(default:false)* - stop any touches on this container from scrolling the page

- **stopPropagation** Boolean *(default:false)* - stop event propagation
Expand All @@ -67,6 +69,7 @@ window.mySwipe = new Swipe(document.getElementById('slider'), {
speed: 400,
auto: 3000,
continuous: true,
threshold: 0.25,
disableScroll: false,
stopPropagation: false,
callback: function(index, elem) {},
Expand Down
7 changes: 4 additions & 3 deletions swipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function Swipe(container, options) {
var index = parseInt(options.startSlide, 10) || 0;
var speed = options.speed || 300;
options.continuous = options.continuous !== undefined ? options.continuous : true;
options.threshold = options.threshold !== undefined ? parseFloat(options.threshold) : 0.5;

function setup() {

Expand Down Expand Up @@ -345,9 +346,9 @@ function Swipe(container, options) {

// determine if slide attempt triggers next/prev slide
var isValidSlide =
Number(duration) < 250 // if slide duration is less than 250ms
&& Math.abs(delta.x) > 20 // and if slide amt is greater than 20px
|| Math.abs(delta.x) > width/2; // or if slide amt is greater than half the width
Number(duration) < 250 // if slide duration is less than 250ms
&& Math.abs(delta.x) > 20 // and if slide amt is greater than 20px
|| (Math.abs(delta.x) / width) > options.threshold; // or if slide amt is greater than threshold

// determine if slide attempt is past start and end
var isPastBounds =
Expand Down