Vue如何實現(xiàn)簡單的時間軸與時間進度條
前言
項目需要按天播放地圖等值線圖功能,所以需要一個時間進度條,網(wǎng)上找了一下發(fā)現(xiàn)沒有自己需要的樣子,于是只能簡單的寫一個。
1、封裝時間尺度組件
<!-- 時間尺度 --> <template> <div class="time"> <div class="time_menu" v-show="timeList.length != 0"> <template v-if="playState"> <el-tooltip class="item" effect="dark" content="暫停" placement="top-end"> <img src="../assets/timescale/zt.png" @click="playState = false,stopInterval()" /> </el-tooltip> </template> <template v-if="!playState"> <el-tooltip class="item" effect="dark" content="播放" placement="top-end"> <img src="../assets/timescale/bf.png" @click="playState = true,startInterval()" /> </el-tooltip> </template> <el-tooltip class="item" effect="dark" content="上一天" placement="top-end"> <img src="../assets/timescale/icons_next.png" @click="upTime()" style="transform: rotate(180deg);" /> </el-tooltip> <el-tooltip class="item" effect="dark" content="下一天" placement="top-end"> <img src="../assets/timescale/icons_next.png" @click="nextTime()" /> </el-tooltip> </div> <div style="width: 80%;height: 100%;position: relative;display: flex;align-items: flex-end;overflow: auto;"> <div style="height: 40%;display: flex;flex-shrink: 0;flex-flow: row nowrap;align-items: flex-end;"> <template v-for="(item,index) in timeList"> <el-tooltip class="item" effect="dark" :content="item" placement="top-end"> <div class="keDuXian" :style="index%5 == 0 ? 'height:100%;':'height:60%;'" :id="item" @click="timeClick(item)"> <template v-if="index > 0"> <div v-if="index%5 == 0" style="position: relative;top: -20px;left:-30px;color: #FFF;width: 70px;font-size: 0.75rem;"> {{item}} </div> </template> </div> </el-tooltip> </template> </div> <div v-show="timeList.length != 0" class="progress" :style="'width:'+progresswidth+'px;'"> <div style="width: 100%;height: 40%;background-color: rgb(20,170,255);"> </div> </div> <img v-show="timeList.length != 0" src="../assets/timescale/xsjx.png" class="progressImg" :style="'left:'+(progresswidth == 0 ? -7 : (progresswidth-8))+'px;'" /> </div> </div> </template> <script> import {getScopeTime} from "../utils/regular.js" export default { data() { return { timeList:[], thisTime: '', progresswidth: 0, playState: false, Interval:'', } }, beforeDestroy(){ clearInterval(this.Interval) }, methods: { startInterval(){ this.Interval = setInterval(() => { if(this.timeList.indexOf(this.thisTime)+1 == this.timeList.length){ this.playState = false this.stopInterval() }else{ this.thisTime = this.timeList[this.timeList.indexOf(this.thisTime) + 1] } this.setProgressWidth() }, 4000) }, stopInterval(){ clearInterval(this.Interval) }, init(time,start,end) { this.timeList = getScopeTime(start,end,2) this.thisTime = time this.$nextTick(()=>{ this.setProgressWidth() }) }, timeClick(time) { this.thisTime = time this.setProgressWidth() }, setProgressWidth(){ this.progresswidth = document.getElementById(this.thisTime).offsetLeft this.$emit('schedule',this.thisTime) }, upTime(){ if(this.thisTime == this.timeList[0]){ this.$message({ message: '已經(jīng)是第一天了', type: 'warning' }); }else{ this.thisTime = this.timeList[this.timeList.indexOf(this.thisTime)-1] this.setProgressWidth() } }, nextTime(){ if(this.thisTime == this.timeList[this.timeList.length-1]){ this.$message({ message: '已經(jīng)是最后一天了', type: 'warning' }); }else{ this.thisTime = this.timeList[this.timeList.indexOf(this.thisTime)+1] this.setProgressWidth() } } } } </script> <style lang="less" scoped> .time { width: 100%; height: 100%; background: rgba(10, 34, 66, 0.65); box-shadow: inset 0px 1px 12px 0px rgba(75, 137, 255, 0.5); border-radius: 4px; border: 1px solid #57C8EE; display: flex; align-items: flex-end; position: relative; } .time_menu { width: 20%; height: 100%; display: flex; align-items: center; justify-content: space-evenly; padding: 0 3%; box-sizing: border-box; img { width: 20px; height: 20px; cursor: pointer; transition-duration: 0.5s; } } .progress { height:100%; position: absolute; display: flex; align-items: flex-end; z-index: -1; transition-duration: 0.5s; } .triangle { width: 0px; height: 0px; border: 20px solid transparent; border-top-color: #00FFFF; // opacity: 1; position: absolute; left: -20px; top: 20px; } .keDuXian{ width: 2px; background-color: #FFF; cursor: pointer; margin-right:25px; } .progressImg{ width: 1.125rem; height: 1.125rem; position: absolute; z-index:9; } </style>
2、在vue頁面使用時間尺度
首先引入組件 然后給組件外部包一層div 組件的大小是根據(jù)父級來的
初始化:在methods方法里調(diào)用組件內(nèi)部的init方法初始化 傳入三個參數(shù)
schedule事件是每當(dāng)尺度變化會返回變化后的時間,可以根據(jù)時間做對應(yīng)邏輯處理
<!-- 進度條--> <div style="width: 50%;height: 4%;position: absolute;z-index: 999;bottom: 20%;left: 25%;"> <timescale ref="timescale" @schedule="schedule"></timescale> </div> <!-- 引入組件--> import timescale from "../../components/timeScale.vue" <!-- 調(diào)用組件內(nèi)部方法 初始化時間尺度 傳入選中時間 起時間 止時間--> this.$refs.timescale.init(this.isOlineTime,this.selectSectionTime[0],getTomorrow(this.selectSectionTime[1]))
3、組件init方法內(nèi) 通過起止時間算出中間的所有時間尺度
startTime:開始時間
endTime:結(jié)束時間
type:1按日返回小時 2按月返回每天
export const getScopeTime = (startTime, endTime, type) => { let start = new Date(startTime).getTime() let end = new Date(endTime).getTime() let time = [] if (type == 2) { for (var i = 0; i < 1; i--) { start += 86400000 if (start == end) { time.unshift(startTime.split(' ')[0]) break } else { time.push(unixTimeToDateTime(start).split(' ')[0]) } } } else if (type == 1) { for (var i = 0; i < 1; i--) { start += 3600000 if (start == end) { time.unshift(startTime.split(' ')[0]) break } else { time.push(unixTimeToDateTime(start)) } } } return time }
附上效果圖
目前沒有實現(xiàn)拖拽功能,只能通過點擊刻度更換時間,或者自動播放。
總結(jié)
到此這篇關(guān)于Vue如何實現(xiàn)簡單的時間軸與時間進度條的文章就介紹到這了,更多相關(guān)Vue實現(xiàn)時間軸內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+Bootstrap實現(xiàn)簡易學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Vue+Bootstrap實現(xiàn)簡易學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-02-02Element?el-date-picker?日期選擇器的使用
本文主要介紹了Element?el-date-picker?日期選擇器的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04elementUI+Springboot實現(xiàn)導(dǎo)出excel功能的全過程
這篇文章主要介紹了elementUI+Springboot實現(xiàn)導(dǎo)出excel功能,現(xiàn)在也對這個導(dǎo)出功能進行一個匯總整理寫出來,結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09vue3中setup語法糖下父子組件間傳遞數(shù)據(jù)的方式
Vue3中父組件指的是包含一個或多個子組件的組件,它們通過props和事件等方式來傳遞數(shù)據(jù)和通信,這篇文章主要介紹了vue3中setup語法糖下父子組件間傳遞數(shù)據(jù)的方式,需要的朋友可以參考下2023-06-06關(guān)于ElementUI el-table 鼠標(biāo)滾動失靈的問題及解決辦法
這篇文章主要介紹了關(guān)于ElementUI el-table 鼠標(biāo)滾動失靈的問題及解決辦法,本文給大家分享問題所在原因及解決方案,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08