Home > Article > Web Front-end > How to implement timeline using Vue?
With the popularity of social networks and other platforms, timelines have gradually become a popular form for people to share their life experiences. The timeline can display a series of events or activities in chronological order to help people review the past and understand history. It can also be used to display personal growth, travel diaries, team project progress, etc.
In web development, if you want to display the timeline, you can use the Vue framework to quickly build and achieve the effect. Let's learn how to use Vue to implement the timeline.
1. Page layout
Timelines are usually divided into two forms: vertical timeline and horizontal timeline. The horizontal timeline generally displays a series of events within a certain time period, while the vertical timeline displays events in chronological order, similar to a timeline.
This article takes the vertical timeline as an example. First, according to the design draft, we need to write the page layout code;
<div class="timeline"> <div class="timeline-header"> <div class="timeline-header-line"></div> </div> <div class="timeline-container"> <div class="timeline-item"> <div class="timeline-item-time">2010年</div> <div class="timeline-item-content">content 1</div> </div> <div class="timeline-item"> <div class="timeline-item-time">2012年</div> <div class="timeline-item-content">content 2</div> </div> <div class="timeline-item"> <div class="timeline-item-time">2015年</div> <div class="timeline-item-content">content 3</div> </div> <div class="timeline-item"> <div class="timeline-item-time">2017年</div> <div class="timeline-item-content">content 4</div> </div> </div> </div>
In order to better display the style, I used flex layout here.
2. Define data and render
Next, you need to define the data in the Vue instance and render the data into the page.
new Vue({ el: '#app', data: { list: [ { time: '2010年', content: 'content 1' }, { time: '2012年', content: 'content 2' }, { time: '2015年', content: 'content 3' }, { time: '2017年', content: 'content 4' } ] } })
Then use the v-for
directive to loop through the data on the page, and use {{}}
to bind the data to the page.
<div class="timeline-item" v-for="item in list"> <div class="timeline-item-time">{{ item.time }}</div> <div class="timeline-item-content">{{ item.content }}</div> </div>
3. Implement animation effects
In order to increase the user experience, we can add animation effects to each event. Add a new attribute isShow
to the list
attribute in data
to identify whether to display the current event content.
data: { list: [ { time: '2010年', content: 'content 1', isShow: false }, { time: '2012年', content: 'content 2', isShow: false }, { time: '2015年', content: 'content 3', isShow: false }, { time: '2017年', content: 'content 4', isShow: false } ] }
Next, you can add a click event to each event to control the display and hiding of the current event content. Here we need to use Vue's event handler and class
binding.
<div class="timeline-item" v-for="(item, index) in list" :key="index"> <div class="timeline-item-time" @click="item.isShow = !item.isShow"> {{ item.time }} </div> <div class="timeline-item-content" :class="{show: item.isShow}">{{ item.content }}</div> </div>
We can add judgment in the class
attribute. If the current item.isShow
is true, add the show
class, otherwise not add it. This enables display and hiding of event content.
.timeline-item-content { display:none; height: 0; transition:all .3s linear; } .show { display:block; height: auto; }
The most basic timeline component can be implemented through the above code.
Finally simply paste the complete code.
<div id="app"> <div class="timeline"> <div class="timeline-header"> <div class="timeline-header-line"></div> </div> <div class="timeline-container"> <div class="timeline-item" v-for="(item, index) in list" :key="index"> <div class="timeline-item-time" @click="item.isShow = !item.isShow"> {{ item.time }} </div> <div class="timeline-item-content" :class="{show: item.isShow}"> {{ item.content }} </div> </div> </div> </div> </div> <style> .timeline { position: relative; max-width: 1000px; margin: 30px auto; } .timeline-header { position: absolute; top: 0; left: 50%; width: 2px; height: 100%; background: #ccc; transform: translate(-50%, calc(50% - 15px)); } .timeline-header-line { position: absolute; top: 0; left: 50%; width: 50px; height: 30px; background: #ccc; transform: translateX(-50%); border-radius: 30px; } .timeline-container { padding: 40px; display: flex; flex-direction: column; justify-content: space-between; position: relative; } .timeline-item { display: flex; flex-wrap: wrap; justify-content: space-between; position: relative; margin-bottom: 50px; padding: 0 50px; cursor: pointer; } .timeline-item:after { content: ''; position: absolute; top: 12px; left: -15px; width: 20px; height: 20px; background: #ccc; border-radius: 50%; } .timeline-item:before { content: ''; position: absolute; top: 0; left: -2px; width: 0; height: 0; border-top: 20px solid transparent; border-bottom: 20px solid transparent; border-left: 20px solid #ccc; } .timeline-item:after, .timeline-item:before { z-index: 2; } .timeline-item-content { display: none; height: 0; transition: all .3s linear; position: relative; z-index: 1; width: 100%; margin-left: 10px; } .timeline-item-time { width: 150px; font-size: 16px; font-weight: bold; color: #333; text-align: right; flex-shrink: 0; margin-right: 20px; } .show { display: block; height: auto; } </style> <script> new Vue({ el: '#app', data: { list: [ { time: '2010年', content: 'content 1', isShow: false }, { time: '2012年', content: 'content 2', isShow: false }, { time: '2015年', content: 'content 3', isShow: false }, { time: '2017年', content: 'content 4', isShow: false } ] } }) </script>
The above is the detailed content of How to implement timeline using Vue?. For more information, please follow other related articles on the PHP Chinese website!