js實(shí)現(xiàn)頁面導(dǎo)航層級(jí)指示效果
我們上網(wǎng)經(jīng)??吹揭恍╉撁鎸蛹?jí)指示的效果,比如淘寶、京東、百度百科等,用于指示用戶瀏覽頁面到哪里了。可以起到很好的交互效果,現(xiàn)在我們就來自己去實(shí)現(xiàn)以下類似的效果。主要用到的API是scrollIntoView 和 getBoundingClientRect方法,原理是監(jiān)聽頁面滾動(dòng)元素,當(dāng)元素距離到瀏覽器視口一定的位置的時(shí)候就證明元素開始出現(xiàn)在頁面的視口,就可以標(biāo)識(shí)右邊的指示欄。
代碼如下:
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>H5 canvas繪制框選截圖和保存截圖代碼</title> <style> *{padding:0;margin:0;} #scrollBox{margin:0 auto;width:1000px;} #scrollBox .scroll-tips{position:fixed;right:10px;top:300px;width:100px;} #scrollBox .scroll-tips li {cursor:pointer;text-align:center;} #scrollBox .scroll-tips li.active {background:#ccc;} #scrollBox .ct {height: 550px;line-height:550px;text-align:center;font-size:40px;color:#fff;background:green;} </style> </head> <body> <div id="scrollBox"> <div class="main"> <div class="content"> <h2 id="title1" class="title-bar" data-id="1">標(biāo)題1</h2> <div class="ct">內(nèi)容1</div> </div> <div class="content"> <h2 id="title2" class="title-bar" data-id="2">標(biāo)題2</h2> <div class="ct" style="height:847px;">內(nèi)容2</div> </div> <div class="content"> <h2 id="title3" class="title-bar" data-id="3">標(biāo)題3</h2> <div class="ct" style="height:547px;">內(nèi)容3</div> </div> <div class="content"> <h2 id="title4" class="title-bar" data-id="4">標(biāo)題4</h2> <div class="ct" style="height:427px;">內(nèi)容4</div> </div> <div class="content"> <h2 id="title5" class="title-bar" data-id="5">標(biāo)題5</h2> <div class="ct" style="height:187px;">內(nèi)容5</div> </div> <div class="content"> <h2 id="title6" class="title-bar" data-id="6">標(biāo)題6</h2> <div class="ct" style="height:232px;">內(nèi)容6</div> </div> </div> <div class="scroll-tips"> <ul> <li id="Li1" class="active" data-id="1">標(biāo)題1</li> <li id="Li2" data-id="2">標(biāo)題2</li> <li id="Li3" data-id="3">標(biāo)題3</li> <li id="Li4" data-id="4">標(biāo)題4</li> <li id="Li5" data-id="5">標(biāo)題5</li> <li id="Li6" data-id="6">標(biāo)題6</li> </ul> </div> </div> <script> let Li = $$('.scroll-tips li'); let scrollTips = $$('.title-bar'); function $$(str) { return document.querySelectorAll(str); }; for (let i = 0, len = Li.length; i < len; i++) { // 點(diǎn)擊右側(cè)標(biāo)識(shí)點(diǎn)左側(cè)滾動(dòng)到窗口顯示位置 Li[i].onclick = function () { let id = this.getAttribute('data-id'); $$('.scroll-tips li.active')[0].classList.remove('active'); this.classList.add('active'); $$('#title' + id)[0].scrollIntoView(); }; }; // 獲取當(dāng)前頁面滾動(dòng)到瀏覽器視口頂部位置的元素 const getScrollTop = function (domArr) { for (let i = 0, len = domArr.length; i < len; i++) { let top = domArr[i].getBoundingClientRect().top; // 表示在一定范圍內(nèi)允許的值 if (top >= 0 && top <= 30) { return domArr[i]; } } }; // 監(jiān)聽滾動(dòng)方法 window.onscroll = function () { let LiId = getScrollTop(scrollTips) && getScrollTop(scrollTips).getAttribute('data-id'); if (LiId) { $$('.scroll-tips li.active')[0].classList.remove('active'); $$('#Li' + LiId)[0].classList.add('active'); } }; </script> </body> </html>
運(yùn)行結(jié)果如下:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript Blob對(duì)象實(shí)現(xiàn)文件下載
這篇文章主要為大家介紹了vue組件通信的幾種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12JS高級(jí)調(diào)試技巧:捕獲和分析 JavaScript Error詳解
前端工程師都知道 JavaScript 有基本的異常處理能力。我們可以 throw new Error(),瀏覽器也會(huì)在我們調(diào)用 API 出錯(cuò)時(shí)拋出異常。但估計(jì)絕大多數(shù)前端工程師都沒考慮過收集這些異常信息2014-03-03JS面向?qū)ο缶幊袒A(chǔ)篇(三) 繼承操作實(shí)例詳解
這篇文章主要介紹了JS面向?qū)ο缶幊汤^承操作,結(jié)合實(shí)例形式詳細(xì)分析了JS面向?qū)ο罄^承的具體概念、原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03hash特點(diǎn)、hashchange事件介紹及其常見應(yīng)用場(chǎng)景
淺析hash特點(diǎn)、hashchange事件介紹及其常見應(yīng)用場(chǎng)景(不同hash對(duì)應(yīng)不同事件處理、移動(dòng)端大圖展示狀態(tài)后退頁面問題、原生輕應(yīng)用頭部后退問題、移動(dòng)端自帶返回按鈕二次確認(rèn)問題),hashchange和popstate事件觸發(fā)條件2023-11-11多種方法實(shí)現(xiàn)load加載完成后把圖片一次性顯示出來
如何一個(gè)load 加載完成后把圖片一次性顯示出來,下面有個(gè)不錯(cuò)的方法,希望對(duì)大家有所幫助2014-02-02微信小程序如何使用canvas二維碼保存至手機(jī)相冊(cè)
這篇文章主要介紹了微信小程序如何使用canvas二維碼保存至手機(jī)相冊(cè)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用微信小程序具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07