Vue如何實(shí)現(xiàn)簡(jiǎn)單的時(shí)間軸與時(shí)間進(jìn)度條
前言
項(xiàng)目需要按天播放地圖等值線(xiàn)圖功能,所以需要一個(gè)時(shí)間進(jìn)度條,網(wǎng)上找了一下發(fā)現(xiàn)沒(méi)有自己需要的樣子,于是只能簡(jiǎn)單的寫(xiě)一個(gè)。
1、封裝時(shí)間尺度組件
<!-- 時(shí)間尺度 --> <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頁(yè)面使用時(shí)間尺度
首先引入組件 然后給組件外部包一層div 組件的大小是根據(jù)父級(jí)來(lái)的
初始化:在methods方法里調(diào)用組件內(nèi)部的init方法初始化 傳入三個(gè)參數(shù)
schedule事件是每當(dāng)尺度變化會(huì)返回變化后的時(shí)間,可以根據(jù)時(shí)間做對(duì)應(yīng)邏輯處理
<!-- 進(jìn)度條--> <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)部方法 初始化時(shí)間尺度 傳入選中時(shí)間 起時(shí)間 止時(shí)間--> this.$refs.timescale.init(this.isOlineTime,this.selectSectionTime[0],getTomorrow(this.selectSectionTime[1]))
3、組件init方法內(nèi) 通過(guò)起止時(shí)間算出中間的所有時(shí)間尺度
startTime:開(kāi)始時(shí)間
endTime:結(jié)束時(shí)間
type:1按日返回小時(shí) 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 }
附上效果圖
目前沒(méi)有實(shí)現(xiàn)拖拽功能,只能通過(guò)點(diǎn)擊刻度更換時(shí)間,或者自動(dòng)播放。
總結(jié)
到此這篇關(guān)于Vue如何實(shí)現(xiàn)簡(jiǎn)單的時(shí)間軸與時(shí)間進(jìn)度條的文章就介紹到這了,更多相關(guān)Vue實(shí)現(xiàn)時(shí)間軸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+Bootstrap實(shí)現(xiàn)簡(jiǎn)易學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Vue+Bootstrap實(shí)現(xiàn)簡(jiǎn)易學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02Element?el-date-picker?日期選擇器的使用
本文主要介紹了Element?el-date-picker?日期選擇器的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04elementUI+Springboot實(shí)現(xiàn)導(dǎo)出excel功能的全過(guò)程
這篇文章主要介紹了elementUI+Springboot實(shí)現(xiàn)導(dǎo)出excel功能,現(xiàn)在也對(duì)這個(gè)導(dǎo)出功能進(jìn)行一個(gè)匯總整理寫(xiě)出來(lái),結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09vue3中setup語(yǔ)法糖下父子組件間傳遞數(shù)據(jù)的方式
Vue3中父組件指的是包含一個(gè)或多個(gè)子組件的組件,它們通過(guò)props和事件等方式來(lái)傳遞數(shù)據(jù)和通信,這篇文章主要介紹了vue3中setup語(yǔ)法糖下父子組件間傳遞數(shù)據(jù)的方式,需要的朋友可以參考下2023-06-06關(guān)于ElementUI el-table 鼠標(biāo)滾動(dòng)失靈的問(wèn)題及解決辦法
這篇文章主要介紹了關(guān)于ElementUI el-table 鼠標(biāo)滾動(dòng)失靈的問(wèn)題及解決辦法,本文給大家分享問(wèn)題所在原因及解決方案,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08詳解vue computed的緩存實(shí)現(xiàn)原理
這篇文章主要介紹了vue computed的緩存實(shí)現(xiàn)原理,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下2021-04-04vue修改對(duì)象的屬性值后頁(yè)面不重新渲染的實(shí)例
今天小編就為大家分享一篇vue修改對(duì)象的屬性值后頁(yè)面不重新渲染的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08