This article shares the specific code of JavaScript to achieve the floor effect for your reference. The specific content is as follows * { 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>I am level 1</li> <li>I am level 2</li> <li>I am level 3</li> <li>I am level 4</li> <li>I am level 5</li> </ul> <ol> <li class="selected">Layer 1</li> <li>Layer 2</li> <li>Layer 3</li> <li>Layer 4</li> <li>Layer 5</li> </ol> js: // 1. Initialize the color of the floor 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. Select whoever is clicked let oItems = document.querySelectorAll("ol>li"); let currentItem = oItems[0]; // Get the height of the visible area 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; // Implement scrolling // window.scrollTo(0, i * screenHeight); // Note: Use documentElement.scrollTop to scroll the web page. Do not add units when setting the value. // 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); } } //Get the browser viewport width and height 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 } } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: 20 CSS coding tips to make you more efficient (sorted)
>>: 4 Practical Tips for Web Page Design
A brief analysis of rem First of all, rem is a CS...
Table of contents 1. Setup 1. The first parameter...
1. Filter Example: <!DOCTYPE html> <html...
This article shares with you a draggable photo wa...
What I have been learning recently involves knowl...
This article describes how to install Apache on a...
Introduction to jQuery The jQuery library can be ...
Table of contents 1. Scope 2. Scope Chain 3. Lexi...
In HTML pages, we sometimes need to automatically ...
The figure below shows the browser viewing rate i...
Docker-compose deployment configuration jenkins 1...
Table of contents 1 Promise Interrupt the call ch...
We know that in general, a function must be calle...
Table of contents When declaring multiple variabl...
Background Threads •Master Thread The core backgr...