Skip to content
This repository was archived by the owner on Jan 19, 2023. It is now read-only.

Commit 6958d43

Browse files
Antti Piltofritz-c
authored andcommitted
fix scroll on touchscreen
1 parent 9075095 commit 6958d43

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/react-image-lightbox.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class ReactImageLightbox extends Component {
7575
this.handleOuterMouseDown = this.handleOuterMouseDown.bind(this);
7676
this.handleOuterMouseMove = this.handleOuterMouseMove.bind(this);
7777
this.handleOuterMousewheel = this.handleOuterMousewheel.bind(this);
78+
this.handleOuterTouchStart = this.handleOuterTouchStart.bind(this);
79+
this.handleOuterTouchMove = this.handleOuterTouchMove.bind(this);
7880
this.handleWindowResize = this.handleWindowResize.bind(this);
7981
this.handleZoomInButtonClick = this.handleZoomInButtonClick.bind(this);
8082
this.handleZoomOutButtonClick = this.handleZoomOutButtonClick.bind(this);
@@ -154,17 +156,21 @@ class ReactImageLightbox extends Component {
154156
document.addEventListener('keyup', this.handleKeyInput);
155157
window.addEventListener('resize', this.handleWindowResize);
156158
window.addEventListener('mouseup', this.handleMouseUp);
159+
window.addEventListener('touchend', this.handleMouseUp);
160+
document.addEventListener('touchstart', this.handleOuterTouchStart);
161+
window.addEventListener('touchmove', this.handleOuterTouchMove);
157162

158163
// Have to add an extra mouseup handler to catch mouseup events outside of the window
159164
// if the page containing the lightbox is displayed in an iframe
160165
if (isInIframe()) {
161166
window.top.addEventListener('mouseup', this.handleMouseUp);
167+
window.top.addEventListener('touchend', this.handleMouseUp);
162168
}
163169

164170
this.listenersAttached = true;
165171
}
166172
}
167-
173+
168174
// Change zoom level
169175
changeZoom(zoomLevel, clientX, clientY) {
170176
const windowWidth = getWindowWidth();
@@ -231,9 +237,13 @@ class ReactImageLightbox extends Component {
231237
document.removeEventListener('keyup', this.handleKeyInput);
232238
window.removeEventListener('resize', this.handleWindowResize);
233239
window.removeEventListener('mouseup', this.handleMouseUp);
240+
window.removeEventListener('touchend', this.handleMouseUp);
241+
document.removeEventListener('touchstart', this.handleOuterTouchStart);
242+
window.removeEventListener('touchmove', this.handleOuterTouchMove);
234243

235244
if (isInIframe()) {
236245
window.top.removeEventListener('mouseup', this.handleMouseUp);
246+
window.top.removeEventListener('touchend', this.handleMouseUp);
237247
}
238248

239249
this.listenersAttached = false;
@@ -577,6 +587,39 @@ class ReactImageLightbox extends Component {
577587
}
578588
}
579589

590+
// Touch screen version of handleOuterMouseDown()
591+
handleOuterTouchStart(event) {
592+
// Allow dragging when zoomed
593+
if (this.state.zoomLevel > MIN_ZOOM_LEVEL) {
594+
this.isDragging = true;
595+
let touchObj = event.changedTouches[0];
596+
this.dragStartX = parseInt(touchObj.clientX);
597+
this.dragStartY = parseInt(touchObj.clientY);
598+
this.dragStartOffsetX = this.state.offsetX;
599+
this.dragStartOffsetY = this.state.offsetY;
600+
}
601+
}
602+
603+
// Touch screen version of handleOuterMouseMove()
604+
handleOuterTouchMove(event) {
605+
event.preventDefault();
606+
607+
if(this.state.zoomLevel <= MIN_ZOOM_LEVEL) // We shouldn't go any further if we're not zoomed
608+
return;
609+
610+
const zoomMultiplier = this.getZoomMultiplier();
611+
612+
const touchObj = event.changedTouches[0];
613+
const newOffsetX = (this.dragStartX - parseInt(touchObj.clientX)) / zoomMultiplier + this.dragStartOffsetX;
614+
const newOffsetY = (this.dragStartY - parseInt(touchObj.clientY)) / zoomMultiplier + this.dragStartOffsetY;
615+
if (this.state.offsetX !== newOffsetX || this.state.offsetY !== newOffsetY) {
616+
this.setState({
617+
offsetX: newOffsetX,
618+
offsetY: newOffsetY,
619+
});
620+
}
621+
}
622+
580623
// Handle the window resize event
581624
handleWindowResize() {
582625
clearTimeout(this.resizeTimeout);

0 commit comments

Comments
 (0)