更新时间:2023-11-30 22:50: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 }">
在mousewheel事件中,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
}