基于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中mock數(shù)據(jù),模擬后臺(tái)接口實(shí)例
這篇文章主要介紹了vue中mock數(shù)據(jù),模擬后臺(tái)接口實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue.js中Vue-router 2.0基礎(chǔ)實(shí)踐教程
這篇文章主要給大家介紹了關(guān)于vue.js中Vue-router 2.0基礎(chǔ)實(shí)踐的相關(guān)資料,其中包括vue-router 2.0的基礎(chǔ)用法、動(dòng)態(tài)路由匹配、嵌套路由、編程式路由、命名路由以及命名視圖等相關(guān)知識(shí),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-05-05
vue本地打開(kāi)build后生成的dist文件夾index.html問(wèn)題
這篇文章主要介紹了vue本地打開(kāi)build后生成的dist文件夾index.html問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-09-09
在vue2項(xiàng)目中使用dart-sass的問(wèn)題及解決方法
在Vue2項(xiàng)目中,使用dart-sass替代node-sass可以解決安裝困難和環(huán)境兼容問(wèn)題,VueCLI3+用戶可直接使用,而VueCLI2用戶需升級(jí)VueCLI和項(xiàng)目,具體方法包括修改package.json依賴并使用.scss文件編寫(xiě)樣式,此更改有助于提升項(xiàng)目的開(kāi)發(fā)效率和跨平臺(tái)兼容性2024-09-09
springboot+vue+對(duì)接支付寶接口+二維碼掃描支付功能(沙箱環(huán)境)
這篇文章主要介紹了springboot+vue+對(duì)接支付寶接口+二維碼掃描支付(沙箱環(huán)境),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Vue封裝數(shù)字框組件實(shí)現(xiàn)流程詳解
這篇文章主要介紹了Vue封裝數(shù)字框組件實(shí)現(xiàn)流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-04-04

