jQuery 滑块作为时间线

发布于 2024-09-18 09:29:35 字数 361 浏览 4 评论 0原文

我刚刚完成了 jQuery 手风琴与 jQuery 滑块的合并。 即

显示3张图片。用户可以使用 PREVNEXT 按钮查看下一个/上一个 3 个图像。他们还可以使用滑块浏览所有图像。

下一步是使这个滑块看起来像时间线。左侧需要从 1970 年开始,到 2010 年结束。对于每个项目(在本例中一个项目是一组 3 个图像),我需要它在时间轴上显示日期。

即: alt text

我知道我可以创建一个日期宽度合适的图像,但理想情况下这需要是动态的,所以可以放入更多项目,时间线会自动更新。

I have just finished incorporating a jQuery accordian with a jQuery Slider.
I.e.

3 pictures are displayed. The user can either use PREV or NEXT buttons to view the next/prev 3 images. They can also navigate through all the images with the slider.

The next step is to make this slider look like a timeline. The left hand side needs to start at 1970 and finish at 2010. For each item (an item is a set of 3 images in this case) I need it to show a date on the timeline.

i.e: alt text

I know I could create an image with the dates the right width apart but idealy this needs to be dynamic so more items can be put in and the timeline self updates.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝梦月影 2024-09-25 09:29:35

在较高级别上,以下操作应该有效:

  1. 获取滑块 UI 元素的总宽度(以像素为单位)。
  2. 将此数字除以[标签总数] - 1,即可得到分配给每个标签的像素总数。
  3. 在滑块 div 后立即添加一系列 div,其宽度为步骤 2 中获得的宽度和 float:left 样式。
  4. 在所有内容后面添加一个带有 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);
}

At a high level, the following should work:

  1. Get the total width of the slider UI element, in pixels.
  2. Divide this number by [total number of labels] - 1 to get the total number of pixels to allocate to each label.
  3. Add a series of div's immediately after the slider div with the width you got in step 2 and the float:left style.
  4. Follow everything with an empty div with the clear: both style.

Here is a basic example:

CSS

.timeline {
    width: 500px;
    border: 1px solid black;
}
.timelineEntry {
    float: left;
}
.first {
    position: relative; left: 5px;
}
.last {
    position: relative; left: -10px;
}
.clear {
    clear: both;
}

Markup

<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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文