JS電梯導(dǎo)航的實(shí)現(xiàn)示例
預(yù)覽效果
之前css 利用 scroll-behavior 和 錨點(diǎn) 實(shí)現(xiàn)了 電梯導(dǎo)航,點(diǎn)擊可以看這篇文章css實(shí)現(xiàn)電梯導(dǎo)航的效果。評(píng)論區(qū)有人想讓我用js也實(shí)現(xiàn)一下,一開始我起初有點(diǎn)蒙,不知道css和js實(shí)現(xiàn)的效果有什么區(qū)別。后來我發(fā)現(xiàn)了。
css實(shí)現(xiàn)的只是單純點(diǎn)擊滑動(dòng)到指定位置而已。而js實(shí)現(xiàn)不僅可以實(shí)現(xiàn)點(diǎn)擊滑動(dòng),而且可以監(jiān)聽滑動(dòng)事件,讓滑動(dòng)標(biāo)題高亮,也就是有一個(gè)active類
比如我們?cè)谶@里滑倒了第一塊區(qū)域的末尾,但是還沒滑倒第二塊區(qū)域,于是滑動(dòng)標(biāo)題還只是Section1高亮,當(dāng)我們滑動(dòng)到第二塊區(qū)域時(shí),滑動(dòng)標(biāo)題變成了Section2高亮


主要的js代碼
主要思路就是對(duì)滑動(dòng)事件進(jìn)行監(jiān)聽。監(jiān)聽到當(dāng)前滑動(dòng)到的位置,然后判斷當(dāng)前所在的位置處于哪一塊區(qū)域。給它add active的類,當(dāng)滑走當(dāng)前區(qū)域,就會(huì)remove active類。
監(jiān)聽點(diǎn)擊事件,其實(shí)跟css 里的scroll-behavior差不多。點(diǎn)擊導(dǎo)航,然后獲取點(diǎn)擊的href,然后獲取要顯示的區(qū)域的位置,然后滑動(dòng)到那個(gè)位置,然后導(dǎo)航就會(huì)運(yùn)行上面的監(jiān)聽加高亮類。
// 獲取所有的導(dǎo)航鏈接
const links = document.querySelectorAll('.elevator a');
// 獲取所有的內(nèi)容區(qū)塊
const sections = document.querySelectorAll('.section');
// 監(jiān)聽窗口滾動(dòng)事件
window.addEventListener('scroll', function () {
// 獲取滾動(dòng)條的位置
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
// 遍歷所有的內(nèi)容區(qū)塊
sections.forEach(function (section) {
// 獲取內(nèi)容區(qū)塊的位置信息
const offsetTop = section.offsetTop;
const offsetHeight = section.offsetHeight;
// 判斷當(dāng)前內(nèi)容區(qū)塊是否在可視范圍內(nèi)
if (scrollTop >= offsetTop && scrollTop < offsetTop + offsetHeight) {
// 如果在可視范圍內(nèi),則將對(duì)應(yīng)的導(dǎo)航鏈接設(shè)置為 active 狀態(tài)
links.forEach(function (link) {
if (link.getAttribute('href') === '#' + section.getAttribute('id')) {
link.classList.add('active');
} else {
link.classList.remove('active');
}
});
}
});
});
// 監(jiān)聽所有的導(dǎo)航鏈接的點(diǎn)擊事件
links.forEach(function (link) {
link.addEventListener('click', function (event) {
event.preventDefault();
// 獲取目標(biāo)內(nèi)容區(qū)塊的位置信息
const targetId = link.getAttribute('href');
const targetSection = document.querySelector(targetId);
const targetOffsetTop = targetSection.offsetTop;
// 使用動(dòng)畫滑動(dòng)到目標(biāo)內(nèi)容區(qū)塊
window.scrollTo({
top: targetOffsetTop,
behavior: 'smooth'
});
});
});整體代碼
html
- 導(dǎo)航 一個(gè)ul標(biāo)簽里面五個(gè)li 點(diǎn)擊事件
- 頁面顯示 五個(gè)塊級(jí)區(qū)域
<div class="elevator">
<ul>
<li><a href="#section1" rel="external nofollow" >Section 1</a></li>
<li><a href="#section2" rel="external nofollow" >Section 2</a></li>
<li><a href="#section3" rel="external nofollow" >Section 3</a></li>
<li><a href="#section4" rel="external nofollow" >Section 4</a></li>
<li><a href="#section5" rel="external nofollow" >Section 5</a></li>
</ul>
</div>
<div class="section" id="section1">
<h2>Section 1</h2>
<p>第一塊 </p>
<video
src="https://img-baofun.zhhainiao.com/pcwallpaper_ugc/preview/2366564fa6b83158208eb3181752a8d6_preview.mp4"
autoplay muted loop></video>
</div>
<div class="section" id="section2">
<h2>Section 2</h2>
<p>第二塊 </p>
<video src="https://img-baofun.zhhainiao.com/market/133/3760b2031ff41ca0bd80bc7a8a13f7bb_preview.mp4" autoplay
muted loop></video>
</div>
<div class="section" id="section3">
<h2>Section 3</h2>
<p>第三塊 </p>
<video src="https://img-baofun.zhhainiao.com/market/semvideo/3fc6cdef4427e61be69096c6ebb59a1c_preview.mp4"
autoplay muted loop></video>
</div>
<div class="section" id="section4">
<h2>Section 4</h2>
<p>第四塊 </p>
<video src="https://img-baofun.zhhainiao.com/market/semvideo/6ac24b3f50fda0b1a55f7ff25c6b62af_preview.mp4"
autoplay muted loop></video>
</div>
<div class="section" id="section5">
<h2>Section 5</h2>
<p>第五塊 </p>
<video src="https://img-baofun.zhhainiao.com/market/133/97ba6b60662ab4f31ef06cdf5a5f8e94_preview.mp4" autoplay
muted loop></video>
</div>css
.elevator 固定電梯按鈕,也就是導(dǎo)航
.elevator {
position: fixed; /* 固定定位,保證電梯一直在頁面可視區(qū)域內(nèi) */
top: 50%; /* 在頁面垂直居中 */
right: 0; /* 在頁面右側(cè) */
transform: translateY(-50%); /* 通過 translateY 屬性來調(diào)整位置,保證垂直居中 */
}
.elevator ul {
list-style: none; /* 去掉列表樣式 */
margin: 0; /* 去掉外邊距 */
padding: 0; /* 去掉內(nèi)邊距 */
}
.elevator li {
margin: 10px 0; /* 設(shè)置每個(gè)列表項(xiàng)的上下外邊距 */
}
.elevator a {
display: block; /* 將鏈接轉(zhuǎn)換為塊級(jí)元素,方便設(shè)置樣式 */
padding: 10px; /* 設(shè)置內(nèi)邊距 */
background-color: #ccc; /* 設(shè)置背景顏色 */
color: #000; /* 設(shè)置文字顏色 */
text-decoration: none; /* 去掉下劃線 */
}
.elevator a.active {
background-color: #000; /* 當(dāng)前所在樓層鏈接的背景顏色 */
color: #fff; /* 當(dāng)前所在樓層鏈接的文字顏色 */
}
/* 頁面區(qū)塊樣式 */
.section {
width: 90vw; /* 頁面寬度為視口寬度的90% */
height: 110vh; /* 頁面高度為視口高度的110% */
}
.section video {
width: 100%; /* 視頻寬度自適應(yīng)父級(jí)容器 */
height: 90%; /* 視頻高度為父級(jí)容器高度的90% */
}js
就是之前的主要的js代碼
到此這篇關(guān)于JS電梯導(dǎo)航的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)JS電梯導(dǎo)航內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JS中用三種方式實(shí)現(xiàn)導(dǎo)航菜單中的二級(jí)下拉菜單
- JS實(shí)現(xiàn)選中當(dāng)前菜單后高亮顯示的導(dǎo)航條效果
- 一個(gè)js控制的導(dǎo)航菜單實(shí)例代碼
- JS 實(shí)現(xiàn)導(dǎo)航欄懸停效果
- js實(shí)現(xiàn)移動(dòng)端導(dǎo)航點(diǎn)擊自動(dòng)滑動(dòng)效果
- JavaScript實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊導(dǎo)航欄變色特效
- js實(shí)現(xiàn)無限級(jí)樹形導(dǎo)航列表效果代碼
- JavaScript實(shí)現(xiàn)滑動(dòng)導(dǎo)航欄效果
- js實(shí)現(xiàn)水平滾動(dòng)菜單導(dǎo)航
- 原生JS實(shí)現(xiàn)導(dǎo)航下拉菜單效果
相關(guān)文章
微信小程序?qū)崿F(xiàn)收藏與取消收藏切換圖片功能
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)收藏與取消收藏切換圖片功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
JavaScript中防抖和節(jié)流的原理和區(qū)別詳解
JavaScript 中,防抖和節(jié)流是一種用于優(yōu)化事件處理函數(shù)調(diào)用頻率的技術(shù),防抖和節(jié)流的目的都是為了避免頻繁地觸發(fā)事件處理函數(shù),從而減少瀏覽器和服務(wù)器的負(fù)擔(dān),本文將給大家介紹一下JavaScript中防抖和節(jié)流的原理和區(qū)別,需要的朋友可以參考下2023-09-09
JS之Date對(duì)象和獲取系統(tǒng)當(dāng)前時(shí)間詳解
本篇文章主要是對(duì)JS之Date對(duì)象和獲取系統(tǒng)當(dāng)前時(shí)間進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01
JavaScript實(shí)現(xiàn)帶箭頭標(biāo)識(shí)的多級(jí)下拉菜單效果
這篇文章主要介紹了JavaScript實(shí)現(xiàn)帶箭頭標(biāo)識(shí)的多級(jí)下拉菜單效果,可實(shí)現(xiàn)橫向與縱向箭頭的形式標(biāo)識(shí)選中菜單項(xiàng)位置的功能,涉及javascript針對(duì)頁面元素位置的判定與樣式動(dòng)態(tài)操作技巧,需要的朋友可以參考下2015-08-08
如何利用javascript接收json信息并進(jìn)行處理
這篇文章主要介紹了如何利用javascript接收json信息并進(jìn)行處理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
關(guān)于javascript event flow 的一個(gè)bug詳解
描述了firefox,safari 有一個(gè)bug和DOM 3 規(guī)范不一致:在event.currentTarget等于event.target的時(shí)候(即event flow處于target phase時(shí)),會(huì)調(diào)用添加到currentTarget上的useCapture為true的listener2013-09-09

