How to use HTML, CSS and jQuery to make a dynamic timeline, specific code examples are required
Timeline is a common way to display the time sequence and event flow method, very suitable for displaying historical events, project progress, etc. Using HTML, CSS and jQuery technology, you can easily create a dynamic timeline effect. This article will introduce how to use these techniques to achieve a simple timeline effect and provide specific code examples.
First, we need to create a basic timeline structure in HTML. The following is a code example:
In the above HTML code, we created a.timeline
container, which contains a.timeline-items
container and a.timeline-progress
Progress bar..timeline-items
The container is used to place events on the timeline. Each event is represented by.timeline-item
, and the details of the event are placed in.timeline-item- content
Container.
Next, we use CSS styles to beautify the appearance of the timeline. The following is a code example:
.timeline { position: relative; margin: 50px auto; width: 800px; } .timeline-items { position: relative; } .timeline-item { position: relative; margin-bottom: 50px; padding: 20px; background: #f1f1f1; } .timeline-item-content { display: inline-block; vertical-align: top; } .timeline-progress { position: absolute; width: 4px; background: #666; top: 0; bottom: 0; left: 50%; transform: translateX(-50%); }
In the above CSS code, we set the basic style of the.timeline
container and beautify.timeline-item
and ## The appearance of #.timeline-progress.
$(document).ready(function() { var timelineItems = $(".timeline-items .timeline-item"); var progress = $(".timeline-progress"); // 设置进度条的初始位置 progress.css("height", timelineItems.length * 100); // 监听滚动事件,更新进度条位置 $(window).scroll(function() { var scrollTop = $(this).scrollTop(); var windowHeight = $(this).height(); var documentHeight = $(document).height(); var timelineOffset = $(".timeline").offset().top; var progressHeight = windowHeight * ((scrollTop - timelineOffset) / (documentHeight - windowHeight)); progress.css("top", scrollTop - timelineOffset); progress.css("height", progressHeight); }); });
The above is the detailed content of How to make a dynamic timeline using HTML, CSS and jQuery. For more information, please follow other related articles on the PHP Chinese website!