jquery实现蛇形时间轴
时间: 2023-08-12 09:20:55 浏览: 152
首先,可以使用HTML和CSS来创建蛇形时间轴的基本结构和样式,然后使用jQuery来操纵它。
下面是一个简单的示例代码:
HTML结构:
```html
<div class="timeline">
<div class="timeline-item">
<div class="timeline-content">
<h2>2010</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h2>2012</h2>
<p>Donec ut metus eget ligula blandit commodo.</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h2>2015</h2>
<p>Suspendisse potenti. Nulla facilisi.</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h2>2018</h2>
<p>Phasellus vel semper velit, eget varius ex.</p>
</div>
</div>
</div>
```
CSS样式:
```css
.timeline {
position: relative;
margin: 0 auto;
padding: 0;
list-style: none;
max-width: 800px;
}
.timeline:before {
content: '';
position: absolute;
top: 0;
bottom: 0;
width: 2px;
background-color: #ddd;
left: 50%;
margin-left: -1px;
}
.timeline-item {
margin-bottom: 50px;
position: relative;
}
.timeline-item:before,
.timeline-item:after {
content: '';
display: table;
}
.timeline-item:after {
clear: both;
}
.timeline-item:last-child {
margin-bottom: 0;
}
.timeline-content {
position: relative;
width: 50%;
padding: 20px;
background-color: #f7f7f7;
border-radius: 8px;
}
.timeline-content h2 {
margin-top: 0;
}
.timeline-content p {
margin: 0;
}
/* 反转偶数项 */
.timeline-item:nth-child(even) .timeline-content {
float: right;
}
.timeline-item:nth-child(even) .timeline-content:before {
right: auto;
left: -15px;
border-width: 8px 0 8px 15px;
border-color: transparent transparent transparent #f7f7f7;
}
.timeline-item:nth-child(even) .timeline-content:after {
right: auto;
left: 0px;
border-width: 7px 0 7px 15px;
border-color: transparent transparent transparent #ddd;
}
```
jQuery代码:
```javascript
$(document).ready(function() {
var items = $('.timeline-item');
var heights = [];
// 获取所有项目的高度
items.each(function() {
var height = $(this).outerHeight();
heights.push(height);
});
// 设置所有项目的高度
items.each(function(i) {
var height = heights[i];
$(this).css('height', height);
});
});
```
这段代码将获取所有时间轴项目的高度,并将其应用到每个项目上,从而创建一个蛇形时间轴的效果。
阅读全文