在较高的层面上,应采取以下措施:
-
-
把这个数除以
[total number of labels] - 1
获取要分配给每个标签的像素总数。
-
float:left
-
用空的div和
clear: both
风格。
CSS格式
.timeline {
width: 500px;
border: 1px solid black;
}
.timelineEntry {
float: left;
}
.first {
position: relative; left: 5px;
}
.last {
position: relative; left: -10px;
}
.clear {
clear: both;
}
标记
<div id="timelineContainer">
<div class="timeline" id="slider">
Slider UI Goes Here
</div>
</div>
<div class="clear"></div>
var container = document.getElementById("timelineContainer");
var labels = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
var totalWidth = $("#slider").width();
var labelWidth = Math.floor(totalWidth / (labels.length - 1));
for (var index = 0; index < labels.length; index++) {
var nextLabel = document.createElement("div");
nextLabel.className = "timelineEntry";
if (index == 0) {
nextLabel.className += " first";
}
else if (index == labels.length - 1) {
nextLabel.className += " last";
}
nextLabel.style.width = labelWidth + "px";
nextLabel.innerHTML = labels[index];
container.appendChild(nextLabel);
}