Skip to content

Commit 9b41bc2

Browse files
author
Weera Wu
committed
Ignore touch on the center of the slide
1 parent 69de268 commit 9b41bc2

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

index.html

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,31 @@
213213
return Theme[key] === value;
214214
});
215215
},
216+
getTouchDirection: function(x, width) {
217+
if (!x || !width) {
218+
return null;
219+
}
220+
if (x > width * 2 / 3) {
221+
return TouchDirection.RIGHT;
222+
} else if (x > width / 2) {
223+
return TouchDirection.CENTER;
224+
} else {
225+
return TouchDirection.LEFT;
226+
}
227+
},
228+
getTouchType: function() {
229+
if (!state.touch.start) {
230+
return null;
231+
}
232+
var elapsedTime = Date.now() - state.touch.start;
233+
if (elapsedTime > 500) {
234+
return TouchType.HOLD;
235+
} else if (elapsedTime > 250) {
236+
return TouchType.PRESS;
237+
} else {
238+
return TouchType.TAP;
239+
}
240+
},
216241
loadSlides: function(url) {
217242
return m.request({method: 'GET', url: url}).
218243
then(state.setSlides).
@@ -230,19 +255,6 @@
230255
nextTheme: function() {
231256
state.setTheme((state.theme + 1) % Object.keys(Theme).length);
232257
},
233-
touchType: function() {
234-
if (!state.touch.start) {
235-
return null;
236-
}
237-
var elapsedTime = Date.now() - state.touch.start;
238-
if (elapsedTime > 500) {
239-
return TouchType.HOLD;
240-
} else if (elapsedTime > 250) {
241-
return TouchType.PRESS;
242-
} else {
243-
return TouchType.TAP;
244-
}
245-
},
246258
togglePointer: function() {
247259
state.pointer = !state.pointer;
248260
},
@@ -287,10 +299,8 @@
287299
return;
288300
}
289301
var touch = event.touches[0];
290-
var pageCenter = document.body.clientWidth / 2;
291-
state.setTouch(
292-
touch.pageX < pageCenter ? TouchDirection.LEFT : TouchDirection.RIGHT
293-
);
302+
var direction = state.getTouchDirection(touch.pageX, document.body.clientWidth);
303+
state.setTouch(direction);
294304
},
295305
handleTouchmove: function(event) {
296306
event.preventDefault();
@@ -301,7 +311,7 @@
301311
if (event.touches.length > 0) {
302312
return;
303313
}
304-
var type = state.touchType();
314+
var type = state.getTouchType();
305315
var direction = state.touch.direction;
306316
if (type === TouchType.HOLD) {
307317
state.nextTheme();

0 commit comments

Comments
 (0)