Objectives for this periodUse JavaScript to create floor navigation effects and achieve two functions:
1. Function Implementation1.1 Structural Layer<div id="box" class="box"> <ul class="list"> <li class="content-part" data-n="Column 1">Column 1</li> <li class="content-part" data-n="Column 2">Column 2</li> <li class="content-part" data-n="Column Three">Column Three</li> <li class="content-part" data-n="Column 4">Column 4</li> <li class="content-part" data-n="Column Five">Column Five</li> </ul> </div> <div class="left"> <ul id="left-list"> <li data-n="Column 1">Column 1</li> <li data-n="Column 2">Column 2</li> <li data-n="Column Three">Column Three</li> <li data-n="Column 4">Column 4</li> <li data-n="Column Five">Column Five</li> </ul> </div> 1.2 Style Layer<style> * { margin: 0; padding: 0; } body .box { width: 1200px; } body .box { margin: 0 auto; } ul { list-style: none; } body .box ul li { height: 800px; background-color: silver; margin-bottom: 20px; font-size: 30px; font-weight: bold; } body .left { position: fixed; left: 20px; bottom: 100px; width: 100px; height: 250px; top: 50%; margin-top: -125px; background-color: silver; } body .left ul li { height: 50px; line-height: 50px; text-align: center; cursor: pointer; } body .current { color: #fff; background-color: tomato; } </style> 1.3 Behavioral Layer1.3.1 Floor JumpClick the floor button in the left menu to jump to the corresponding floor. var oList = document.getElementById('left-list'); // Click event delegate oList.onclick = function (e) { if (e.target.tagName.toLowerCase() == 'li') { // Get the value of data-n var n = e.target.getAttribute('data-n'); // [] attribute selector var contentPart = document.querySelector('.content-part[data-n=' + n + ']'); //Set scroll document.documentElement.scrollTop = contentPart.offsetTop; } } 1.3.2 Floor MonitoringFloor monitoring, when the page is scrolled, the background of the floor column in the left menu will change accordingly. //When the page scrolls, the background of the box column on the left changes accordingly var contents = document.querySelectorAll('.content-part'); var lis = document.querySelectorAll('#left-list li'); var offsetTopArr = []; for (var i = 0; i < contents.length; i++) { offsetTopArr.push(contents[i].offsetTop); } // For the convenience of comparison, add infinite offsetTopArr.push(Infinity); // Monitor scrolling var nowFloor = -1; window.onscroll = function (e) { var nowScrollTop = document.documentElement.scrollTop; // The i value of break is the subscript of the box array for (var i = 0; i < offsetTopArr.length; i++) { if (nowScrollTop >= offsetTopArr[i] && nowScrollTop < offsetTopArr[i + 1]) { break; } } // Floors are not equal, change the style if (nowFloor != i) { nowFloor = i; for (var j = 0; j < lis.length; j++) { if (j == i) { // Add style to the current floor lis[j].className = 'current'; } else { // Remove the styles of other floors lis[j].className = ''; } } } } 2. Effect preview3. Project CodeClick to go to the code repository to view the complete code. This concludes this article about the detailed process of creating floor navigation effects with JavaScript. For more relevant JavaScript floor navigation content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: XHTML Getting Started Tutorial: XHTML Hyperlinks
>>: How to view image information in Docker
Table of contents 1. Usage of keep-alive Example ...
Table of contents What is a listener in vue Usage...
1. Packaging Vue project Enter the following name...
The key is that the local server does not have wr...
Table of contents Too long to read Component styl...
1. Get the mysql image docker pull mysql:5.6 Note...
First, let's talk about the in() query. It is...
Install Required Files Yum install openssl-* -y C...
background When we talk about transactions, every...
In actual work, JavaScript regular expressions ar...
This article uses examples to describe how to cre...
The conversion between time, string and timestamp...
ElementUI implements the table list paging effect...
There are three ways to interconnect and communic...
First, let me introduce the meaning of some field...