JS實(shí)現(xiàn)時(shí)間軸自動(dòng)播放
最近實(shí)現(xiàn)了一個(gè)這樣的效果,點(diǎn)擊播放按鈕,時(shí)間軸開(kāi)始播放,點(diǎn)擊暫停,停止在當(dāng)前位置,當(dāng)再次點(diǎn)擊播放的時(shí)候,從當(dāng)前位置開(kāi)始繼續(xù)播放,也可以點(diǎn)擊相應(yīng)的年份,切換過(guò)去,這時(shí)候播放自動(dòng)暫停,當(dāng)再次點(diǎn)擊播放的時(shí)候,從當(dāng)前位置出發(fā)。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="fonts/iconfont.css" />
<style scoped>
ul,
li,
html,
body {
margin: 0;
padding: 0;
}
#timeline {
list-style: none;
text-align: center;
background: url('img/xuanduan.png') 9px top repeat-y;
}
#timeline li {
background-image: url('img/tuoyuan1.png');
background-repeat: no-repeat;
background-position: 0 15px;
background-size: 20px 20px;
padding-left: 30px;
height: 50px;
line-height: 50px;
color: #444;
width: 70px;
}
#timeline li p {
width: 70px;
cursor: pointer;
margin: 0;
}
.biaoqian {
background: url('img/biaoqian.png') 2px 13px no-repeat;
;
background-size: 60px 24px;
color: #fff;
}
#timeline .selecteded {
background: url('img/tuoyuan2.png') 0 15px no-repeat;
background-size: 20px 20px;
}
.scroll-shell {
width: 100px;
height: 96%;
margin-top: 1.5%;
margin-left: 20px;
float: left;
overflow: hidden;
}
.scroll {
width: 118px;
height: 103%;
overflow: auto;
overflow-x: hidden;
}
</style>
</head>
<body>
<div class="scroll-shell">
<ul style="" id="timeline" ref="timeline" @click="timeline($event)" class="scroll">
</ul>
<i class="iconfont icon-bofang" id="bofangzanting" style="font-size: 38px;position: absolute;left: 25px;top: 91%;"></i>
</div>
<script>
let years = [2016, 2017, 2018, 2019, 2020, 2021, 2022]
let index = 0
let timer=null
//創(chuàng)建時(shí)間軸對(duì)應(yīng)的li
years.map(k => {
let createLi = document.createElement('li')
let createP = document.createElement('p')
createP.innerHTML = k
createLi.appendChild(createP)
timeline.appendChild(createLi)
})
//默認(rèn)選中第一個(gè)
var lis = document.querySelectorAll('#timeline li')
lis[0].classList.add('selecteded')
var ps = document.querySelectorAll('#timeline li p')
ps[0].classList.add('biaoqian')
//點(diǎn)擊事件,點(diǎn)擊其中一個(gè)切換到相應(yīng)的效果
var ulElement = document.querySelector('#timeline')
ulElement.onclick = function(e) {
var lis = document.querySelectorAll('#timeline li')
var ps = document.querySelectorAll('#timeline li p')
let event = e || window.event
let target = event.target || event.srcElement
if (target.tagName == 'P') {
classChange(ps, lis, target)
for (let i = 0; i < lis.length; i++) {
console.log(lis[i].getAttribute('class'))
if (lis[i].getAttribute('class') == 'selecteded') {
//記住此時(shí)被點(diǎn)擊的索引,方便點(diǎn)擊播放按鈕時(shí)繼續(xù)播放
index = i
console.log(index)
break;
}
}
}
}
//公共部分,清除掉所有的樣式,再給點(diǎn)擊的添加相應(yīng)的類名
function classChange(ps, lis, target) {
ps.forEach(k => {
k.classList.remove('biaoqian')
})
target.classList.add('biaoqian')
lis.forEach(v => {
v.classList.remove('selecteded')
})
target.parentNode.classList.add('selecteded')
}
//播放和暫停按鈕
let bofangzanting = document.getElementById('bofangzanting')
if (bofangzanting) {
bofangzanting.onclick = () => {
if (bofangzanting.className.indexOf('bofang') != -1) {
console.log('點(diǎn)擊播放')
console.log(timer)
bofangzanting.classList.remove('icon-bofang')
bofangzanting.classList.add('icon-zanting')
if (timer == undefined) {
autoPlay()
}
} else {
console.log('點(diǎn)擊暫停')
bofangzanting.classList.remove('icon-zanting')
bofangzanting.classList.add('icon-bofang')
if (timer) {
timer = clearInterval(timer)
} else {
return
}
}
}
}
//自動(dòng)播放
function autoPlay() {
var lis = document.querySelectorAll('#timeline li')
var ps = document.querySelectorAll('#timeline li p')
timer = setInterval(() => {
console.log('自動(dòng)播放啦!')
if (index < ps.length - 1) {
//執(zhí)行下一個(gè)
classChange(ps, lis, ps[index + 1])
index++
} else {
//跳轉(zhuǎn)到開(kāi)始
index = 0
classChange(ps, lis, ps[index])
}
console.log(index)
}, 1000)
}
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js冒泡法和數(shù)組轉(zhuǎn)換成字符串示例代碼
將數(shù)組轉(zhuǎn)換成字符串的方法有很多,想必大家也不會(huì)陌生,下面為大家講解下js冒泡法的使用,感興趣的朋友可以參考下2013-08-08
javascript實(shí)現(xiàn)簡(jiǎn)單的進(jìn)度條
本文給大家分享2個(gè)javascript實(shí)現(xiàn)簡(jiǎn)單的進(jìn)度條,一個(gè)是個(gè)人制作一個(gè)是網(wǎng)友實(shí)現(xiàn)的,都很不錯(cuò),這里推薦給大家。2015-07-07
JavaScript中的for...of和for...in循環(huán)容易遇到的問(wèn)題及解決方法總結(jié)
在 JavaScript 編程中,for...of 和 for...in 是常用的循環(huán)語(yǔ)法,但它們?cè)谑褂脮r(shí)可能會(huì)引發(fā)一些意想不到的問(wèn)題,本文將分享我在使用這兩種循環(huán)時(shí)所遇到的坑和經(jīng)驗(yàn),需要的朋友可以參考下2023-08-08
uniapp實(shí)現(xiàn)審批流程的具體操作步驟
這篇文章主要介紹了uniapp實(shí)現(xiàn)審批流程的具體操作方法,實(shí)現(xiàn)思路大概是需要要定義一個(gè)變量,記錄當(dāng)前激活的步驟,通過(guò)數(shù)組的長(zhǎng)度來(lái)循環(huán)數(shù)據(jù),如果有就采用3元一次進(jìn)行選擇,具體實(shí)現(xiàn)步驟跟隨小編一起看看吧2024-03-03
JS構(gòu)造函數(shù)與原型prototype的區(qū)別介紹
下面小編就為大家?guī)?lái)一篇JS構(gòu)造函數(shù)與原型prototype的區(qū)別介紹。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
我也種棵OO樹(shù)JXTree[js+css+xml]
我也種棵OO樹(shù)JXTree[js+css+xml]...2007-04-04
JavaScript實(shí)現(xiàn)點(diǎn)擊切換驗(yàn)證碼及校驗(yàn)
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)點(diǎn)擊切換驗(yàn)證碼及校驗(yàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
JavaScript 對(duì)象合并的幾種方法小結(jié)
本文主要介紹了JavaScript 對(duì)象合并的幾種方法小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02

