JS實(shí)現(xiàn)時(shí)間軸自動(dòng)播放
最近實(shí)現(xiàn)了一個(gè)這樣的效果,點(diǎn)擊播放按鈕,時(shí)間軸開始播放,點(diǎn)擊暫停,停止在當(dāng)前位置,當(dāng)再次點(diǎn)擊播放的時(shí)候,從當(dāng)前位置開始繼續(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)到開始 index = 0 classChange(ps, lis, ps[index]) } console.log(index) }, 1000) } </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
動(dòng)態(tài)顯示可輸入的字?jǐn)?shù)提示還可以輸入的字?jǐn)?shù)
這篇文章主要介紹了動(dòng)態(tài)顯示可輸入的字?jǐn)?shù)提示還可以輸入的字?jǐn)?shù),需要的朋友可以參考下2014-04-04javascript使用appendChild追加節(jié)點(diǎn)實(shí)例
這篇文章主要介紹了javascript使用appendChild追加節(jié)點(diǎn)的方法,實(shí)例分析了appendChild()函數(shù)增加結(jié)點(diǎn)的使用技巧,需要的朋友可以參考下2015-01-01JS運(yùn)動(dòng)特效之完美運(yùn)動(dòng)框架實(shí)例分析
這篇文章主要介紹了JS運(yùn)動(dòng)特效之完美運(yùn)動(dòng)框架,結(jié)合實(shí)例形式分析了javascript針對(duì)運(yùn)動(dòng)中的元素屬性檢測(cè)與判斷相關(guān)操作技巧,需要的朋友可以參考下2018-01-01你可能不知道的JavaScript的new Function()方法
JavaScript的精神領(lǐng)袖Douglas Crockford曾說(shuō)過(guò)JavaScript是程序員唯一不需要學(xué)習(xí)就能直接使用的語(yǔ)言. 在編程中確實(shí)是如此2014-04-04javascript實(shí)現(xiàn)分欄顯示小技巧附圖
考試頁(yè)面可以實(shí)現(xiàn)隱藏左邊的考生信息部分,學(xué)了javascript后也能實(shí)現(xiàn)這個(gè)功能了,下面是實(shí)現(xiàn)思路、代碼及解效果截圖,喜歡的朋友們可以看看2014-10-10JS比較兩個(gè)時(shí)間大小的簡(jiǎn)單示例代碼
本篇文章主要介紹了JS比較兩個(gè)時(shí)間大小的簡(jiǎn)單示例代碼。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12js實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊文本框自動(dòng)選中內(nèi)容的方法
這篇文章主要介紹了js實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊文本框自動(dòng)選中內(nèi)容的方法,涉及javascript鼠標(biāo)點(diǎn)擊事件onClick及選擇事件select的使用技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-08-08el-popover嵌套select彈窗點(diǎn)擊實(shí)現(xiàn)自定義關(guān)閉功能
el-popover彈窗內(nèi)嵌套下拉選擇框,點(diǎn)擊el-popover彈出外部區(qū)域需關(guān)閉彈窗,點(diǎn)擊查詢、重置需關(guān)閉彈窗,遇到這樣的需求怎么解決呢,下面小編給大家介紹el-popover嵌套select彈窗點(diǎn)擊實(shí)現(xiàn)自定義關(guān)閉功能,感興趣的朋友一起看看吧2024-07-07