JavaScript實(shí)現(xiàn)樓層效果
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)樓層效果的具體代碼,供大家參考,具體內(nèi)容如下
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
}
ul {
width: 100%;
height: 100%;
}
ul>li {
list-style: none;
width: 100%;
height: 100%;
font-size: 100px;
text-align: center;
}
ol {
position: fixed;
left: 10px;
top: 50%;
transform: translateY(-50%);
}
ol>li {
list-style: none;
width: 100px;
line-height: 40px;
text-align: center;
border: 1px solid #000;
}
.selected {
background: skyblue;
}
<ul>
<li>我是第1層</li>
<li>我是第2層</li>
<li>我是第3層</li>
<li>我是第4層</li>
<li>我是第5層</li>
</ul>
<ol>
<li class="selected">第1層</li>
<li>第2層</li>
<li>第3層</li>
<li>第4層</li>
<li>第5層</li>
</ol>
js:
// 1.初始化樓層的顏色
let oPages = document.querySelectorAll("ul>li");
let colorArr = ['green', 'blue', 'purple', 'red', 'yellow'];
for (let i = 0; i < oPages.length; i++) {
let page = oPages[i];
page.style.background = colorArr[i];
}
// 2.實(shí)現(xiàn)點(diǎn)擊誰就選中誰
let oItems = document.querySelectorAll("ol>li");
let currentItem = oItems[0];
// 獲取可視區(qū)域的高度
let screenHeight = getScreen().height;
let timerId = null;
for (let i = 0; i < oItems.length; i++) {
let item = oItems[i];
item.onclick = function() {
currentItem.className = "";
this.className = "selected";
currentItem = this;
// 實(shí)現(xiàn)滾動(dòng)
// window.scrollTo(0, i * screenHeight);
// 注意點(diǎn): 通過documentElement.scrollTop來實(shí)現(xiàn)網(wǎng)頁滾動(dòng), 在設(shè)置值的時(shí)候不能添加單位
// document.documentElement.scrollTop = i * screenHeight + "px";
// document.documentElement.scrollTop = i * screenHeight;
clearInterval(timerId);
timerId = setInterval(function() {
let begin = document.documentElement.scrollTop;
let target = i * screenHeight;
let step = (target - begin) * 0.2;
begin += step;
if (Math.abs(Math.floor(step)) <= 1) {
clearInterval(timerId);
document.documentElement.scrollTop = i * screenHeight;
return;
}
document.documentElement.scrollTop = begin;
}, 50);
}
}
//獲取瀏覽器視口寬高
function getScreen() {
let width, height;
if (window.innerWidth) {
width = window.innerWidth;
height = window.innerHeight;
} else if (document.compatMode === "BackCompat") {
width = document.body.clientWidth;
height = document.body.clientHeight;
} else {
width = document.documentElement.clientWidth;
height = document.documentElement.clientHeight;
}
return {
width: width,
height: height
}
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaScript制作樓層導(dǎo)航效果流程詳解
- js實(shí)現(xiàn)樓層滾動(dòng)效果
- JS實(shí)現(xiàn)網(wǎng)站樓層導(dǎo)航效果代碼實(shí)例
- JS實(shí)現(xiàn)導(dǎo)航欄樓層特效
- AngularJS實(shí)現(xiàn)的錨點(diǎn)樓層跳轉(zhuǎn)功能示例
- JS實(shí)現(xiàn)留言板功能[樓層效果展示]
- 純html+css+javascript實(shí)現(xiàn)樓層跳躍式的頁面布局(實(shí)例代碼)
- js實(shí)現(xiàn)樓層導(dǎo)航功能
- js實(shí)現(xiàn)樓層效果的簡單實(shí)例
相關(guān)文章
JS實(shí)現(xiàn)點(diǎn)擊上移下移LI行數(shù)據(jù)的方法
這篇文章主要介紹了JS實(shí)現(xiàn)點(diǎn)擊上移下移LI行數(shù)據(jù)的方法,涉及javascript針對LI列表動(dòng)態(tài)排序的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
JS實(shí)現(xiàn)旋轉(zhuǎn)木馬式圖片輪播效果
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)旋轉(zhuǎn)木馬式圖片輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
JavaScript中reduce()的5個(gè)基本用法示例
這篇文章主要給大家介紹了關(guān)于JavaScript中reduce()的5個(gè)基本用法示例,文中通過示例代碼以及圖文介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用js具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
js控制輸入框獲得和失去焦點(diǎn)時(shí)狀態(tài)顯示的方法
這篇文章主要介紹了js控制輸入框獲得和失去焦點(diǎn)時(shí)狀態(tài)顯示的方法,可實(shí)現(xiàn)判斷輸入框的焦點(diǎn)狀態(tài)設(shè)置不同樣式的功能,是非常實(shí)用的技巧,需要的朋友可以參考下2015-01-01
JavaScript實(shí)現(xiàn)淘寶網(wǎng)圖片的局部放大功能
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)淘寶網(wǎng)圖片的局部放大功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
基于JavaScript實(shí)現(xiàn)div層跟隨滾動(dòng)條滑動(dòng)
項(xiàng)目需求是這樣的:在一個(gè)頁面放2個(gè)懸浮框,懸浮框隨頁面的上下滾動(dòng)有上下波動(dòng)的效果,最終固定在同一位置,下面通過本文給大家分享基于JavaScript實(shí)現(xiàn)div層跟隨滾動(dòng)條滑動(dòng)的相關(guān)資料,對js div跟隨滾動(dòng)條滑動(dòng)相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01

