vue實(shí)現(xiàn)橫向時(shí)間軸組件方式
前言
項(xiàng)目中有需要用到橫向時(shí)間軸,網(wǎng)上大部分組件不滿足 功能需要,于是自己寫了一個(gè)。先上簡(jiǎn)單的demo。
功能
- 默認(rèn)獲取初始數(shù)據(jù)顯示對(duì)應(yīng)的時(shí)間軸和時(shí)間點(diǎn)。
- 當(dāng)超出屏幕后,滑動(dòng)滾動(dòng)條加載更多頁(yè),類似分頁(yè)加載。
- 某個(gè)大的時(shí)間軸節(jié)點(diǎn)鼠標(biāo)放入需要顯示相關(guān)信息。
- 某兩個(gè)大節(jié)點(diǎn)之間會(huì)有子節(jié)點(diǎn)出現(xiàn)。
- 每個(gè)子節(jié)點(diǎn)會(huì)有對(duì)應(yīng)的子節(jié)點(diǎn)詳情內(nèi)容展示,無(wú)詳情內(nèi)容介紹的只展示子節(jié)點(diǎn)。
效果圖
vue時(shí)間軸視頻效果

代碼
Timeline組件封裝
<template>
<ul class="timeline-wrapper" @scroll="scrollEvent">
<li class="timeline-item" v-for="item in timelineList" :key="item.id">
<div class="timeline-box">
<div class="out-circle">
<div class="in-circle"></div>
<div class="timeline-date">
<el-popover
placement="bottom"
title="標(biāo)題"
width="200"
trigger="hover"
:content="item.content"
>
<el-button type="text" slot="reference" class="father-text">{{
item.date
}}</el-button>
</el-popover>
</div>
</div>
<div
class="long-line"
v-show="item.isShow"
:style="`width:${
item.children ? (item.children.length + 1) * 100 : 1 * 100
}px`"
>
<div
v-for="(subItem, index) in item.children"
:key="subItem.id"
class="sub-item-box"
>
<span>{{ subItem.name + ":" + subItem.num }}人</span>
<!-- 根據(jù)奇數(shù)偶數(shù)來(lái)判斷向上還是向下 -->
<div
:class="`sub-line-box ${
index % 2 == 0 ? 'top-line-box' : 'bottom-line-box'
}`"
v-show="subItem.content"
>
<div
:class="`children-line-box ${
index % 2 == 0 ? 'top-line' : 'bottom-line'
}`"
></div>
<div
:class="`children-box ${
index % 2 == 0 ? 'top-children-box' : 'bottom-children-box'
}`"
>
{{ subItem.content }}
</div>
</div>
</div>
</div>
</div>
</li>
</ul>
</template>
<script type="text/babel">
import Vue from "vue";
export default Vue.component("Timeline", {
name: "Timeline",
props: {
timelineList: {
type: Array,
default: () => {
return [];
},
},
},
mounted() {},
methods: {
scrollEvent(e) {
this.$emit("scrollEvent", e);
},
handleBottomClick() {
this.$emit("handleBottomClick");
},
},
});
</script>
<style scoped lang="scss">
ul.timeline-wrapper {
list-style: none;
margin: 0;
padding: 0;
padding: 200px 20px;
white-space: nowrap;
overflow-x: scroll;
}
/* 時(shí)間線 */
.timeline-item {
position: relative;
display: inline-block;
.timeline-box {
text-align: center;
// position: absolute;
display: flex;
align-items: center;
.out-circle {
width: 16px;
height: 16px;
background: rgba(14, 116, 218, 0.3);
box-shadow: 0px 4px 12px 0px rgba(0, 0, 0, 0.4);
/*opacity: 0.1;*/
border-radius: 50%;
display: flex;
align-items: center;
cursor: pointer;
.in-circle {
width: 8px;
height: 8px;
margin: 0 auto;
background: rgba(14, 116, 218, 1);
border-radius: 50%;
box-shadow: 0px 4px 12px 0px rgba(0, 0, 0, 0.1);
}
.timeline-date {
color: #333;
margin-top: 40px;
.father-text {
font-weight: 900;
font-size: 16px;
margin-left: -15px;
}
}
}
.long-line {
// width: 300px;
height: 2px;
background: rgba(14, 116, 218, 0.2);
box-shadow: 0px 4px 12px 0px rgba(0, 0, 0, 0.3);
display: flex;
flex-direction: revert;
justify-content: space-around;
.sub-item-box {
margin-top: -20px;
position: relative;
.sub-line-box {
// cursor: pointer;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
.children-line-box {
width: 0px;
border-left: 1px solid rgba(14, 116, 218, 0.3);
}
.children-box {
flex-wrap: wrap;
display: flex;
justify-content: center;
align-items: center;
border: 1px solid rgba(14, 116, 218, 0.3);
white-space: break-spaces;
text-align: center;
padding: 5px;
}
}
.top-line-box {
margin-top: -100px;
height: 60px;
}
.bottom-line-box {
margin-top: 5px;
height: 150px;
}
.top-line {
height: 65px;
}
.bottom-line {
height: 120px;
}
.top-children-box {
margin-top: -90px;
// height: 30px;
width: 100px;
}
.bottom-children-box {
// height: 120px;
width: 150px;
}
}
}
}
.timeline-content {
box-sizing: border-box;
margin-left: 20px;
height: 106px;
padding: 0 0 0 20px;
text-align: left;
margin-bottom: 30px;
.timeline-title {
font-size: 14px;
word-break: break-all;
margin-bottom: 16px;
color: #333;
font-weight: 500;
/*display: inline;*/
}
.timeline-desc {
font-size: 14px;
color: #999999;
}
}
}
.timeline-item:last-of-type .timeline-content {
margin-bottom: 0;
}
</style>父組件引用:
<template>
<Timeline :timelineList="timeLineArr" @scrollEvent="scrollEvent" />
</template>
<script>
import Timeline from "@/components/Timeline";
export default {
components: {
Timeline,
},
computed: {},
data() {
return {
nomore: false,
// 初始話模擬數(shù)據(jù),數(shù)據(jù)較多時(shí)即可,形成滾動(dòng)條。
timeLineArr: [
{
id: 1,
date: "2015",
title: "2015",
content: "2015年初,團(tuán)隊(duì)在北京注冊(cè)公司",
isShow: true,
children: [],
},
{
id: 2,
date: "2016",
title: "2016",
content: "2016年,公司成立銷售團(tuán)隊(duì)",
isShow: true,
children: [
{
name: "創(chuàng)始團(tuán)隊(duì)",
num: 5,
content: "前期公司規(guī)劃",
},
],
},
{
id: 3,
date: "2017",
title: "2017",
content: "2017年,公司決定創(chuàng)建自有品牌,進(jìn)行規(guī)模擴(kuò)招團(tuán)隊(duì)",
isShow: true,
children: [
{
name: "銷售部",
num: 10,
content: "負(fù)責(zé)市場(chǎng)開發(fā)",
},
{
name: "技術(shù)部",
num: 20,
content: "前端:5人,后端10人,測(cè)試5人",
},
],
},
{
id: 4,
date: "2018",
title: "2018",
content: "2018年,新增兩個(gè)部門",
isShow: true,
children: [
{
name: "人力資源部",
num: 3,
content: "負(fù)責(zé)人才招聘",
},
{
name: "財(cái)務(wù)部",
num: 2,
content: "財(cái)務(wù)結(jié)算",
},
{
name: "總裁辦",
num: 2,
content: "",
},
],
},
{
id: 5,
date: "2019",
title: "2019",
content: "2019年",
isShow: true,
children: [
{
name: "商務(wù)企劃部",
num: 2,
content: "對(duì)外合作",
},
],
},
],
};
},
methods: {
// 滾動(dòng)監(jiān)聽
scrollEvent(e) {
if (
e.srcElement.scrollLeft + e.srcElement.clientWidth >=
e.srcElement.scrollWidth
) {
console.log("嘿嘿我在底部觸發(fā)了");
// 這里正常請(qǐng)求數(shù)據(jù)即可
let data = [
{
id: 12,
date: "2020",
title: "2020",
content: "2020年,受全球疫情影響,公司暫未擴(kuò)招人員",
isShow: true,
children: [],
},
{
id: 22,
date: "2021",
title: "2021",
content: "公司被xxx投資公司注入資本1000萬(wàn),公司天使輪融資成功",
isShow: true,
children: [
{
name: "倉(cāng)儲(chǔ)部",
num: 30,
content: "負(fù)責(zé)貨物存儲(chǔ)",
},
{
name: "物流部",
num: 40,
content: "負(fù)責(zé)自有配送",
},
],
},
{
id: 23,
date: "2022",
title: "2022",
content: "公司進(jìn)入A輪融資,公司被xx投資公司注入資本8000萬(wàn)。",
isShow: false,
children: [],
},
];
if (!this.nomore) {
this.timeLineArr[this.timeLineArr.length - 1].isShow = true;
this.timeLineArr.push(...data);
this.nomore = true;
}
}
},
},
};
</script>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue在index.html中引入靜態(tài)文件不生效問題及解決方法
這篇文章主要介紹了vue在index.html中引入靜態(tài)文件不生效問題及解決方法,本文給大家分享兩種原因分析,通過實(shí)例代碼講解的非常詳細(xì) ,需要的朋友可以參考下2019-04-04
vue + node如何通過一個(gè)Txt文件批量生成MP3并壓縮成Zip
這篇文章主要介紹了vue + node如何通過一個(gè)Txt文件批量生成MP3并壓縮成Zip的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
vue搜索高亮popsearch組件的實(shí)現(xiàn)示例
有時(shí)候給頁(yè)面內(nèi)容添加一個(gè)關(guān)鍵詞搜索功能,如果搜索結(jié)果能夠像瀏覽器搜索一樣高亮顯示,那找起來(lái)就會(huì)很明顯體驗(yàn)會(huì)好很多,本文就來(lái)介紹一下vue搜索高亮popsearch組件的實(shí)現(xiàn)示例,感興趣的可以了解一下2023-09-09
Vue使用provide各種傳值后inject獲取undefined的問題及解決
這篇文章主要介紹了Vue使用provide各種傳值后inject獲取undefined的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue+canvas實(shí)現(xiàn)簡(jiǎn)易的九宮格手勢(shì)解鎖器
這篇文章主要為大家詳細(xì)介紹了如何流vue+canvas實(shí)現(xiàn)一個(gè)簡(jiǎn)易的九宮格手勢(shì)解鎖器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09
詳解vue與后端數(shù)據(jù)交互(ajax):vue-resource
本篇文章主要介紹了詳解vue與后端數(shù)據(jù)交互(ajax):vue-resource,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
基于Vue+elementUI實(shí)現(xiàn)動(dòng)態(tài)表單的校驗(yàn)功能(根據(jù)條件動(dòng)態(tài)切換校驗(yàn)格式)
這篇文章主要介紹了Vue+elementUI的動(dòng)態(tài)表單的校驗(yàn)(根據(jù)條件動(dòng)態(tài)切換校驗(yàn)格式),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04

