jQuery时间轴(Timeline)是一种用于展示时间序列数据的交互式UI组件。它通常用于显示历史事件、项目进度、新闻动态等。时间轴可以以多种形式呈现,如水平线、垂直线或时间气泡等。
以下是一个简单的jQuery水平时间轴示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Timeline</title>
<style>
.timeline {
position: relative;
max-width: 600px;
margin: 0 auto;
}
.timeline::before {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 50%;
width: 6px;
background: #ccc;
}
.timeline-item {
position: relative;
margin-bottom: 20px;
}
.timeline-item::before {
content: attr(data-date);
position: absolute;
left: 50%;
transform: translateX(-50%);
font-weight: bold;
}
.timeline-content {
margin-left: 20px;
}
</style>
</head>
<body>
<div class="timeline">
<div class="timeline-item" data-date="2020-01-01">
<div class="timeline-content">
New Year's Day
</div>
</div>
<div class="timeline-item" data-date="2020-02-14">
<div class="timeline-content">
Valentine's Day
</div>
</div>
<div class="timeline-item" data-date="2020-12-25">
<div class="timeline-content">
Christmas Day
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('.timeline-item').click(function() {
$(this).toggleClass('active');
});
});
</script>
</body>
</html>
append
或html
方法动态添加内容,并确保事件绑定在内容更新后进行。通过以上示例和解决方法,您可以快速搭建一个简单的jQuery时间轴,并解决常见的开发问题。
领取专属 10元无门槛券
手把手带您无忧上云