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

prevent the vertical scroll when swipe in mobile device #764

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 17 additions & 31 deletions src/lory.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* globals jQuery */

import detectPrefixes from './utils/detect-prefixes.js';
import supportsPassive from './utils/detect-supportsPassive';
import dispatchEvent from './utils/dispatch-event.js';
import defaults from './defaults.js';

Expand All @@ -25,7 +24,7 @@ export function lory (slider, opts) {

let index = 0;
let options = {};
let touchEventParams = supportsPassive() ? { passive: true } : false;
let touchEventParams = false;

/**
* if object is jQuery convert to native DOM element
Expand Down Expand Up @@ -157,17 +156,17 @@ export function lory (slider, opts) {

if (typeof nextIndex !== 'number') {
if (direction) {
if (infinite && index + (infinite * 2) !== slides.length) {
nextIndex = index + (infinite - index % infinite);
} else {
nextIndex = index + slidesToScroll;
}
if (infinite && index + (infinite * 2) !== slides.length) {
nextIndex = index + (infinite - index % infinite);
} else {
nextIndex = index + slidesToScroll;
}
} else {
if (infinite && index % infinite !== 0) {
nextIndex = index - index % infinite;
} else {
nextIndex = index - slidesToScroll;
}
if (infinite && index % infinite !== 0) {
nextIndex = index - index % infinite;
} else {
nextIndex = index - slidesToScroll;
}
}
}

Expand Down Expand Up @@ -305,9 +304,14 @@ export function lory (slider, opts) {
}

frame.addEventListener('touchstart', onTouchstart, touchEventParams);
frame.addEventListener('touchmove', onTouchmove, touchEventParams);
frame.addEventListener('touchend', onTouchend);

if (enableMouseEvents) {
frame.addEventListener('mousedown', onTouchstart);
frame.addEventListener('mousemove', onTouchmove);
frame.addEventListener('mouseup', onTouchend);
frame.addEventListener('mouseleave', onTouchend);
frame.addEventListener('click', onClick);
}

Expand Down Expand Up @@ -440,18 +444,8 @@ export function lory (slider, opts) {
}

function onTouchstart (event) {
const {enableMouseEvents} = options;
const touches = event.touches ? event.touches[0] : event;

if (enableMouseEvents) {
frame.addEventListener('mousemove', onTouchmove);
frame.addEventListener('mouseup', onTouchend);
frame.addEventListener('mouseleave', onTouchend);
}

frame.addEventListener('touchmove', onTouchmove, touchEventParams);
frame.addEventListener('touchend', onTouchend);

const {pageX, pageY} = touches;

touchOffset = {
Expand Down Expand Up @@ -483,6 +477,7 @@ export function lory (slider, opts) {
}

if (!isScrolling && touchOffset) {
event.preventDefault();
translate(position.x + delta.x, 0, null);
}

Expand Down Expand Up @@ -538,15 +533,6 @@ export function lory (slider, opts) {

touchOffset = undefined;

/**
* remove eventlisteners after swipe attempt
*/
frame.removeEventListener('touchmove', onTouchmove);
frame.removeEventListener('touchend', onTouchend);
frame.removeEventListener('mousemove', onTouchmove);
frame.removeEventListener('mouseup', onTouchend);
frame.removeEventListener('mouseleave', onTouchend);

dispatchSliderEvent('on', 'touchend', {
event
});
Expand Down