从您的原始代码来看,问题似乎是您正在寻找窗口上的垂直滚动(window.scrollY);但由于您的内容永远不会高于窗口,因此什么都不会发生。
如果你将你的容器包装在另一个 div 中,玩转溢出,并为包装器添加一个监听器,你可以实现你想要的(我认为)。
我用here 的答案想出了一个可行的小提琴:
http://jsfiddle.net/LSJxk/6/
HTML:
<div id="wrap">
<div id="container">
<div id="landingpage">
<p>Landing Page</p>
</div>
<div id="galone" class="vert"></div>
<div id="galtwo" class="vert"></div>
<div id="galthree" class="vert"></div>
<div id="galfour" class="vert"></div>
<div id="galfive" class="vert"></div>
<div id="galsix" class="vert"></div>
</div>
</div>
CSS:
#wrap {
width: 100%;
height: 100%;
overflow-x: hidden;
overflow-y: hidden;
}
#container {
position: relative;
height: 200px;
width: 1000px; // the width of all your divs added together
}
jQuery:
var scroller = {};
scroller.e = document.getElementById("wrap");
if (scroller.e.addEventListener) {
scroller.e.addEventListener("mousewheel", MouseWheelHandler, false);
scroller.e.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
} else scroller.e.attachEvent("onmousewheel", MouseWheelHandler);
function MouseWheelHandler(e) {
console.log('scroll');
// cross-browser wheel delta
var e = window.event || e;
var delta = - 20 * (Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail))));
var pst = $('#wrap').scrollLeft() + delta;
if (pst < 0) {
pst = 0;
} else if (pst > $('#container').width()) {
pst = $('#container').width();
}
$('#wrap').scrollLeft(pst);
return false;
}
但请注意其中的陷阱:侦听器位于鼠标滚轮上,这在触摸屏上不起作用。这将使您的网站在 iPhone/iPad/Surfaces/其他任何设备上无用,除非您包含另一个侦听器,或者将现有侦听器包装在条件中,这样它就不会在支持触摸屏的设备上触发。