我有一个阵列,我做了一些事情,但现在我试图在“页面”之间划分它(更像幻灯片)。
我使用.each()
方法遍历它,调用这段冗长的代码将信息放在右页(每页只有5个项目,至少现在为止)。
有没有办法简化这段代码?
理想情况下它可以无限地继续下去:
if (index > 0 && index <= 5) {
var page = $('#librarian-page-gallery-1');
} else if (index > 5 && index <= 10) {
var page = $('#librarian-page-gallery-2');
} else if (index > 10 && index <= 15) {
var page = $('#librarian-page-gallery-3');
} else if (index > 15 && index <= 20) {
var page = $('#librarian-page-gallery-4');
} else if (index > 20 && index <= 25) {
var page = $('#librarian-page-gallery-5');
} else if (index > 25 && index <= 30) {
var page = $('#librarian-page-gallery-6');
} else if (index > 30 && index <= 35) {
var page = $('#librarian-page-gallery-7');
} else if (index > 35 && index <= 40) {
var page = $('#librarian-page-gallery-8');
} else if (index > 45 && index <= 50) {
var page = $('#librarian-page-gallery-9');
}
答案 0 :(得分:5)
例如:
var page = $('#librarian-page-gallery-' + Math.ceil(index/5));
说明:
index/5 builds a float number :
1 -> 0.2
4 -> 0.8
5 -> 1
6 -> 1.2
and Math.ceil returns the nearest greater or equal integer :
0.2 -> 1
0.8 -> 1
1 -> 1
1.2 -> 2