更新时间:2023-11-30 22:24:28
感谢 Andrey 指出您应该使用鼠标滚轮事件而不是滚动事件.
Hat tip to Andrey for pointing out that you should use the mousewheel event rather than the scroll event.
<div class="container" data-bind="foreach: items, event: { mousewheel: scrolled }">
从鼠标滚轮事件中,deltaY
会给你滚动方向,你可以用它来改变被选中的项目:
From the mousewheel event, deltaY
will give you the scroll direction, which you can use to change which item is selected:
scrolled: function (vm, event) {
var direction = Math.sign(event.deltaY), // 1 is down, -1 is up
maxIdx = vm.items().length - 1,
selectedIdx = vm.items.indexOf(vm.selected()) + direction;
if (selectedIdx >= 0 && selectedIdx <= maxIdx) {
vm.selected(vm.items()[selectedIdx]);
}
// return false; // implied as long as you don't return true
}