在高层次上,以下应该起作用:
- 获取滑块 UI 元素的总宽度,以像素为单位。
- 将此数字除以
[total number of labels] - 1
得到分配给每个标签的像素总数。
- 在滑块 div 之后立即添加一系列 div,其中包含您在步骤 2 中获得的宽度和
float:left
样式。
- 使用带有
clear: both
样式的空 div 跟随所有内容。
这是一个基本示例:
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>
JavaScript
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);
}