我想减小这个 if 语句的大小,以便它可以连续迭代直到 20 左右,其模式如下:
if (i >= 0 && i <= 3) {
$('.timeline__bar').css({
transform: 'translateX(-0vw)',
});
}
if (i > 3 && i <= 7) {
$('.timeline__bar').css({
transform: 'translateX(-92vw)',
});
}
if (i > 7 && i <= 11) {
$('.timeline__bar').css({
transform: 'translateX(-184vw)',
});
}
//ect ect until 20
我每次变换的量是 -92 vh 单位,我确信我也可以连接一个循环模式,但我不确定如何做到这一点。
最佳答案
尝试这样的事情
function trans(num) {
$('.timeline__bar').css({
transform: 'translateX(-'+(Math.floor(num/4)*92)+'vw)',
});
}
更新:我使用的是 parseInt,但更改为 Math.floor,因为如果循环它的性能会更高。
关于javascript - 我怎样才能浓缩这些 if 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49369041/