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!

WhentheVue.jsVirtualDOMdetectsachange,itupdatestheVirtualDOM,diffsit,andappliesminimalchangestotherealDOM.ThisprocessensureshighperformancebyavoidingunnecessaryDOMmanipulations.

Vue.js' VirtualDOM is both a mirror of the real DOM, and not exactly. 1. Create and update: Vue.js creates a VirtualDOM tree based on component definitions, and updates VirtualDOM first when the state changes. 2. Differences and patching: Comparison of old and new VirtualDOMs through diff operations, and apply only the minimum changes to the real DOM. 3. Efficiency: VirtualDOM allows batch updates, reduces direct DOM operations, and optimizes the rendering process. VirtualDOM is a strategic tool for Vue.js to optimize UI updates.

Vue.js and React each have their own advantages in scalability and maintainability. 1) Vue.js is easy to use and is suitable for small projects. The Composition API improves the maintainability of large projects. 2) React is suitable for large and complex projects, with Hooks and virtual DOM improving performance and maintainability, but the learning curve is steeper.

The future trends and forecasts of Vue.js and React are: 1) Vue.js will be widely used in enterprise-level applications and have made breakthroughs in server-side rendering and static site generation; 2) React will innovate in server components and data acquisition, and further optimize the concurrency model.

Netflix's front-end technology stack is mainly based on React and Redux. 1.React is used to build high-performance single-page applications, and improves code reusability and maintenance through component development. 2. Redux is used for state management to ensure that state changes are predictable and traceable. 3. The toolchain includes Webpack, Babel, Jest and Enzyme to ensure code quality and performance. 4. Performance optimization is achieved through code segmentation, lazy loading and server-side rendering to improve user experience.

Vue.js is a progressive framework suitable for building highly interactive user interfaces. Its core functions include responsive systems, component development and routing management. 1) The responsive system realizes data monitoring through Object.defineProperty or Proxy, and automatically updates the interface. 2) Component development allows the interface to be split into reusable modules. 3) VueRouter supports single-page applications to improve user experience.

The main disadvantages of Vue.js include: 1. The ecosystem is relatively new, and third-party libraries and tools are not as rich as other frameworks; 2. The learning curve becomes steep in complex functions; 3. Community support and resources are not as extensive as React and Angular; 4. Performance problems may be encountered in large applications; 5. Version upgrades and compatibility challenges are greater.

Netflix uses React as its front-end framework. 1.React's component development and virtual DOM mechanism improve performance and development efficiency. 2. Use Webpack and Babel to optimize code construction and deployment. 3. Use code segmentation, server-side rendering and caching strategies for performance optimization.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools
