我目前正在尝试通过以下方式检查当前事件索引是否等于一个数字:
8、16、24、32、40 等等...如果是这种情况,那么控制台应该打印出:Go down。
上面描述的部分工作正常,但我似乎无法让相反的部分工作。
假设我的 currentIndex 等于 12,那么下一个可能下降的索引将是 16(工作正常),下一个可能上升的索引将是 9,因为它每次只显示 8 个项目。 现在我需要检查 currentIndex 是否等于 8 + 1/16 + 1/24 + 1/32 + 1/40 + 1 ... 所以控制台会打印出 Go up.
如何创建我的 else if
来实现这一目标?
var shownItems = 8;
var currentIndex = 12;
// Check if the currentIndex equals 8 / 16 / 24 / 32 / 40 ...
if(currentIndex / shownItems % 1 === 0) {
console.log("Go down");
}
// Check if the currentIndex equals 8 - 7 / 16 - 7 / 24 - 7 / 32 - 7 / 40 - 7
else if() {
console.log("Go up");
}
最佳答案
只需检查:
(currentIndex + 1) % shownItems === 0
关于Javascript 检查索引是否等于数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49924896/