基于vue實(shí)現(xiàn)8小時(shí)帶刻度的時(shí)間軸根據(jù)當(dāng)前時(shí)間實(shí)時(shí)定位功能
效果圖:
需求:
1 開(kāi)始時(shí)間、結(jié)束時(shí)間可配置
2 時(shí)差固定8小時(shí)
3 根據(jù)當(dāng)前時(shí)間初始化位置
4 每隔5s刷新位置
5 超過(guò)結(jié)束時(shí)間停止刷新
HTML:
<div class="time-axis"> <div class="startTime">{{start_time}}</div> <div class="endTime">{{end_time}}</div> <!-- 小時(shí)刻度 --> <div class="hourLine"> <div class="line" v-for="index of 8" :key="index" :style="{left: `${90*(index-1)}px`}"> <div class="secondLine"> <div class="second" v-for="index of 4" :key="index" :style="{left: `${18*(index-1)}px`}"></div> </div> </div> </div> <!-- 指針 --> <div class="point" :style="{left: `${current_position}px`}" v-if="pointFlag"> <div class="currentTime">{{current_time}}</div> <img src="@/assets/img/gateway/timeLine_point.png" alt=""> </div> </div>
JS:
props: { start_time: { type: String, default: '', }, end_time: { type: String, default: '', }, currentTimeFromP: { type: String, default: '', }, }, data() { return { current_time: '10:00:00', allSecond: 0, st_second: '', et_second: '', current_second: '', current_position: '', timer: null, pointFlag: true, } }, beforeDestroy() { if(this.timer) { clearInterval(this.timer); } }, mounted() { this.st_second = this.hmsToS(this.start_time) this.et_second = this.hmsToS(this.end_time) // 8小時(shí)總秒 this.allSecond = this.hmsToS(this.end_time) - this.hmsToS(this.start_time) // 計(jì)算當(dāng)前位置 this.current_position = this.bibliography() * 722 // 判斷當(dāng)前時(shí)間是否超過(guò)結(jié)束時(shí)間或者小于開(kāi)始時(shí)間 if(this.current_second>=this.et_second || this.current_second<this.st_second ) { this.$message.warning('當(dāng)前時(shí)間不在服務(wù)范圍內(nèi)') this.pointFlag = false } else { this.timer = setInterval(() => { if(this.current_second>=this.et_second || this.current_second<this.st_second ) { clearInterval(this.timer) this.$message.warning('當(dāng)前時(shí)間不在服務(wù)范圍內(nèi)') this.pointFlag = false } this.current_position = this.bibliography() * 722 }, 5000) } }, methods: { // 比例 = (當(dāng)前時(shí)間 - 開(kāi)始時(shí)間) / 總秒數(shù) bibliography() { // 當(dāng)前時(shí)間秒數(shù) this.current_second = this.hmsToS(this.nowDataToHMS()) let key = (this.current_second - this.st_second) / this.allSecond return key }, // 時(shí)分秒轉(zhuǎn)化秒 hmsToS(e) { var time = e; var len= time.split(':') if(len.length==3){ var hour = time.split(':')[0]; var min = time.split(':')[1]; var sec = time.split(':')[2]; return Number(hour*3600) + Number(min*60) + Number(sec); } if(len.length==2){ var min = time.split(':')[0]; var sec = time.split(':')[1]; return Number(min*60) + Number(sec); } if(len.length==1){ var sec = time.split(':')[0]; return Number(sec); } }, // 當(dāng)前時(shí)間時(shí)分秒 nowDataToHMS() { let myDate = new Date() let str = myDate.toTimeString() this.current_time = str.substring(0,8) return this.current_time }, },
CSS:
.time-axis { position: relative; height: 26px; border-left: 2px solid rgba(255,255,255,.6); border-right: 2px solid rgba(255,255,255,.6); width: 720px; background: rgba(0,0,0,.2); color: #fff; .startTime { position: absolute; top: -20px; left: -25px; color: #fff; } .endTime { position: absolute; top: -20px; right: -25px; color: #fff; } .hourLine { height: 70%; width: 100%; position: absolute; bottom: 0; display: flex; .line { position: absolute; width: 90px; height: 100%; border-right: 1px solid rgba(255,255,255,.6); &:nth-child(8) { border-right: none; } .secondLine { height: 50%; width: 100%; position: absolute; bottom: 0; display: flex; .second{ position: absolute; width: 18px; height: 100%; border-right: 1px solid rgba(255,255,255,.3); } } } } .point { position: absolute; left: -8px; .currentTime { position: absolute; bottom: -10px; left: -18px; color: #00D4FF; } img { width: 16px; height: 46px; } } }
到此這篇關(guān)于基于vue實(shí)現(xiàn)8小時(shí)帶刻度的時(shí)間軸根據(jù)當(dāng)前時(shí)間實(shí)時(shí)定位功能的文章就介紹到這了,更多相關(guān)vue帶刻度時(shí)間軸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue如何動(dòng)態(tài)獲取當(dāng)前時(shí)間
- vue怎樣獲取當(dāng)前時(shí)間,并且傳遞給后端(不用注解)
- vue 使用moment獲取當(dāng)前時(shí)間及一月前的時(shí)間
- Vue 中如何利用 new Date() 獲取當(dāng)前時(shí)間
- vue中實(shí)現(xiàn)當(dāng)前時(shí)間echarts圖表時(shí)間軸動(dòng)態(tài)的數(shù)據(jù)(實(shí)例代碼)
- Vue 按照創(chuàng)建時(shí)間和當(dāng)前時(shí)間顯示操作(剛剛,幾小時(shí)前,幾天前)
- Vue 中獲取當(dāng)前時(shí)間并實(shí)時(shí)刷新的實(shí)現(xiàn)代碼
- Vue filter 過(guò)濾當(dāng)前時(shí)間 實(shí)現(xiàn)實(shí)時(shí)更新效果
- vue 2.1.3 實(shí)時(shí)顯示當(dāng)前時(shí)間,每秒更新的方法
- element-ui vue input輸入框自動(dòng)獲取焦點(diǎn)聚焦方式
- Vue3新增時(shí)自動(dòng)獲取當(dāng)前時(shí)間的操作方法
相關(guān)文章
vue路由第二次進(jìn)入頁(yè)面created和mounted不執(zhí)行問(wèn)題及解決
這篇文章主要介紹了vue路由第二次進(jìn)入頁(yè)面created和mounted不執(zhí)行問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12解決echarts中橫坐標(biāo)值顯示不全(自動(dòng)隱藏)問(wèn)題
這篇文章主要介紹了解決echarts中橫坐標(biāo)值顯示不全(自動(dòng)隱藏)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07Vue3解決Mockjs引入后并訪問(wèn)404(Not Found) 的頁(yè)面報(bào)錯(cuò)問(wèn)題
mock.js:是一款模擬數(shù)據(jù)生成器,可以生成隨機(jī)數(shù)據(jù),攔截 Ajax 請(qǐng)求,使用mockjs模擬后端接口,可隨機(jī)生成所需數(shù)據(jù),模擬對(duì)數(shù)據(jù)的增刪改查,本文給大家介紹了Vue3解決Mockjs引入后并訪問(wèn)404(Not Found) 的頁(yè)面報(bào)錯(cuò)問(wèn)題,需要的朋友可以參考下2025-04-04vue @ ~ 相對(duì)路徑 路徑別名設(shè)置方式
這篇文章主要介紹了vue @ ~ 相對(duì)路徑 路徑別名設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Vue項(xiàng)目打包并部署nginx服務(wù)器的詳細(xì)步驟
vue項(xiàng)目開(kāi)發(fā)好之后需要部署到服務(wù)器上進(jìn)行外網(wǎng)訪問(wèn),下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目打包并部署nginx服務(wù)器的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07