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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the <script> tag in the HTML file that refers to the Vue file.

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values for each element; and the .map method can convert array elements into new arrays.
