我正在尝试在JavaScript中进行一些基本的水平滚动劫持,但我没有运气。我看起来像这样的JavaScript
window.onwheel = onWheel;
function onWheel(e) {
if (e.deltaY != 0) {
e.preventDefault();
e.stopPropagation();
var scrollX = scrollJack(e.deltaX, e.deltaY);
window.scrollBy(scrollX, 0);
}
// var inFirstPage = e.target.id == 'canvas';
}
function scrollJack(dx, dy) {
// because we're scrolling horizontally, this should put all of the dy into dx
// return the future value of dx
var magnitude = Math.pow(dx * dx + dy * dy, 0.5);
// a positive dy implies scrolling down
// if dy is positive, go right
var direction = ((dy + dx) > 0) ? 1 : -1;
return magnitude * direction;
}
我不明白为什么它没有提供预期的结果。如果有帮助,我在这里做了一个jsfiddle:https://jsfiddle.net/6q0qanv5/
我也没有尝试使用任何jQuery,我只是想了解我当前的“解决方案”缺失/错误