我试图使用this timeline 但目前它正在计算导航' left'基于日期差异的价值。
我试图让左边的值增加25,因此导航项会有“离开”字样。值25,50,75 ......
这是我尝试过的,但它不起作用 -
function setDatePosition(timelineComponents, min) {
for (i = 0; i < timelineComponents['timelineDates'].length; i++) {
var distance = daydiff(timelineComponents['timelineDates'][0], timelineComponents['timelineDates'][i]),
distanceNorm = Math.round(distance/timelineComponents['eventsMinLapse']) + 2;
timelineComponents['timelineEvents'].eq(i).css('left', distanceNorm*min+'px');
}
}
到
function setDatePosition(timelineComponents, min) {
for (i = 0; i < timelineComponents['timelineDates'].length; i += 25) {
var distance = daydiff(timelineComponents['timelineDates'][0], timelineComponents['timelineDates'][i]),
distanceNorm = Math.round(distance/timelineComponents['eventsMinLapse']) + 2;
timelineComponents['timelineEvents'].eq(i).css('left', i +'%');
}
}
如何将左值增加25而不是当前日期差值计算?
答案 0 :(得分:0)
问题是,您正在尝试访问timelineComponents ['timelineEvents']的第i个元素(通过使用timelineComponents ['timelineEvents']。eq(i))。正如你所指定的那样,每增加25个循环,这意味着你将访问元素0,元素25,元素50等。我相信你要做的是访问每个元素,只需创建'left'属性增加i(以px?为单位)
所以你的功能看起来像这样:
function setDatePosition(timelineComponents, min) {
var position = 0;
for (i = 0; i < timelineComponents['timelineDates'].length; i++) {
var distance = daydiff(timelineComponents['timelineDates'][0], timelineComponents['timelineDates'][i]),
distanceNorm = Math.round(distance/timelineComponents['eventsMinLapse']) + 2;
timelineComponents['timelineEvents'].eq(i).css('left', position +'px');
position += 25;
}
}
如果您每次需要增加25%,只需将位置+'px'部分更改为位置+'%'