forked from Public/pics
Multi-finger gestures like pinch-to-zoom were triggering photo navigation because the touch handler didn't check touch count. Move swipe detection to touchend, reject multi-touch gestures, and require a minimum 40px horizontal distance. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
88 lines
2.1 KiB
JavaScript
88 lines
2.1 KiB
JavaScript
function enableKeyDownNavigation() {
|
|
document.addEventListener("keydown", function (event) {
|
|
if (event.keyCode == 37) {
|
|
var target = document.getElementById("previous_photo").href;
|
|
if (target) {
|
|
event.preventDefault();
|
|
document.location.href = target;
|
|
}
|
|
}
|
|
else if (event.keyCode == 39) {
|
|
var target = document.getElementById("next_photo").href;
|
|
if (target) {
|
|
event.preventDefault();
|
|
document.location.href = target;
|
|
}
|
|
}
|
|
}, false);
|
|
}
|
|
|
|
function disableKeyDownPropagation(obj) {
|
|
for (var x = 0; x < obj.length; x++) {
|
|
obj[x].addEventListener("keydown", function (event) {
|
|
if (event.keyCode == 37 || event.keyCode == 39) {
|
|
event.stopPropagation();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function enableTouchNavigation() {
|
|
var x_down = null;
|
|
var y_down = null;
|
|
var cancelled = false;
|
|
|
|
document.addEventListener('touchstart', function(event) {
|
|
if (event.touches.length > 1) {
|
|
cancelled = true;
|
|
return;
|
|
}
|
|
x_down = event.touches[0].clientX;
|
|
y_down = event.touches[0].clientY;
|
|
cancelled = false;
|
|
}, false);
|
|
|
|
document.addEventListener('touchmove', function(event) {
|
|
if (event.touches.length > 1) {
|
|
cancelled = true;
|
|
}
|
|
}, false);
|
|
|
|
document.addEventListener('touchend', function(event) {
|
|
if (cancelled || x_down === null || y_down === null) {
|
|
x_down = null;
|
|
y_down = null;
|
|
return;
|
|
}
|
|
|
|
var x_diff = x_down - event.changedTouches[0].clientX;
|
|
var y_diff = y_down - event.changedTouches[0].clientY;
|
|
|
|
x_down = null;
|
|
y_down = null;
|
|
|
|
if (Math.abs(x_diff) < 40 || Math.abs(y_diff) > 50) {
|
|
return;
|
|
}
|
|
|
|
if (Math.abs(x_diff) > Math.abs(y_diff)) {
|
|
if (x_diff > 0) {
|
|
var target = document.getElementById("previous_photo").href;
|
|
if (target) {
|
|
document.location.href = target + '#photo_frame';
|
|
}
|
|
} else {
|
|
var target = document.getElementById("next_photo").href;
|
|
if (target) {
|
|
document.location.href = target + '#photo_frame';
|
|
}
|
|
}
|
|
}
|
|
}, false);
|
|
}
|
|
|
|
enableKeyDownNavigation();
|
|
enableTouchNavigation();
|
|
disableKeyDownPropagation(document.getElementsByTagName("textarea"));
|
|
disableKeyDownPropagation(document.getElementsByTagName("input"));
|