Skip to content

Commit ceb6101

Browse files
begnersherwinski
authored and
sherwinski
committed
feat(mobile): add touch delay to allow scroll on mobile (#315)
1 parent 8485e6c commit ceb6101

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ var options = {
108108
hoverDelay: 0,
109109
// An optional number that determines how long to wait before
110110
// showing the ZoomPane because of a `touchstart` event.
111-
// It's unlikely that you would want to use this option, since
112-
// "tap and hold" is much more intentional than a hover event.
111+
// Setting this to a reasonable amount will allow users to execute
112+
// scroll-gestures events on touch-enabled devices with the image as
113+
// a starting point
113114
touchDelay: 0,
114115
// If true, a bounding box will show the area currently being previewed
115116
// during mouse hover

src/js/Trigger.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ export default class Trigger {
6161
event.preventDefault();
6262
}
6363

64+
_preventDefaultAllowTouchScroll(event) {
65+
if (!this.settings.touchDelay || !this._isTouchEvent(event) || this.isShowing) {
66+
event.preventDefault();
67+
}
68+
}
69+
70+
_isTouchEvent(event) {
71+
return !!event.touches;
72+
}
73+
6474
_bindEvents() {
6575
this.settings.el.addEventListener("mouseenter", this._handleEntry, false);
6676
this.settings.el.addEventListener("mouseleave", this._hide, false);
@@ -94,7 +104,7 @@ export default class Trigger {
94104
}
95105

96106
_handleEntry(e) {
97-
e.preventDefault();
107+
this._preventDefaultAllowTouchScroll(e);
98108
this._lastMovement = e;
99109

100110
if (e.type == "mouseenter" && this.settings.hoverDelay) {
@@ -134,7 +144,7 @@ export default class Trigger {
134144

135145
_hide(e) {
136146
if (e) {
137-
e.preventDefault();
147+
this._preventDefaultAllowTouchScroll(e);
138148
}
139149

140150
this._lastMovement = null;
@@ -157,7 +167,7 @@ export default class Trigger {
157167

158168
_handleMovement(e) {
159169
if (e) {
160-
e.preventDefault();
170+
this._preventDefaultAllowTouchScroll(e);
161171
this._lastMovement = e;
162172
} else if (this._lastMovement) {
163173
e = this._lastMovement;

0 commit comments

Comments
 (0)