我有一个switch
语句,在该语句中,我尝试将键盘快捷键映射到水平的整页滚动:
这是我的尝试,不起作用:
switch (event.code) { case "Space" || "PageDown" || "ArrowRight": { scrollAmount += window.innerWidth break } case "PageUp" || "ArrowLeft": { scrollAmount -= window.innerWidth break } case "Home" || "ArrowUp": { scrollAmount = 0 break } case "End" || "ArrowDown": { scrollAmount = container.scrollWidth break } }
在这种情况下如何正确使用运算符?
[我有一个switch语句,在其中我尝试将键盘快捷键映射到水平的整页滚动:空格键或Page Down或向右箭头向前滚动Page Up或向左箭头向后滚动...
您应分别指定每种情况:
switch (event.code) {
case "Space":
case "PageDown":
case "ArrowRight": {
scrollAmount += window.innerWidth
break
}
case "PageUp":
case "ArrowLeft": {
scrollAmount -= window.innerWidth
break
}
case "Home":
case "ArrowUp": {
scrollAmount = 0
break
}
case "End":
case "ArrowDown": {
scrollAmount = container.scrollWidth
break
}
}