Detailed explanation of the process of creating floor navigation effects with JavaScript

Detailed explanation of the process of creating floor navigation effects with JavaScript

Objectives for this period

Use JavaScript to create floor navigation effects and achieve two functions:

  • Floor Jump
  • Floor monitoring

1. Function Implementation

1.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 Layer

1.3.1 Floor Jump

Click 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 Monitoring

Floor 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 preview

insert image description here

3. Project Code

Click 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:
  • js to achieve floor scrolling effect
  • JS code example to achieve website floor navigation effect
  • JS realizes the floor special effects of the navigation bar
  • Example of anchor point floor jump function implemented by AngularJS
  • JS realizes the message board function [floor effect display]
  • Pure HTML+CSS+JavaScript to achieve floor-jumping page layout (example code)
  • js to realize floor navigation function
  • A simple example of implementing floor effects using js
  • JavaScript to achieve floor effect

<<:  XHTML Getting Started Tutorial: XHTML Hyperlinks

>>:  How to view image information in Docker

Recommend

How to customize at and cron scheduled tasks in Linux

There are two types of scheduled tasks in Linux s...

Solution to the error reported by Mysql systemctl start mysqld

Error message: Job for mysqld.service failed beca...

MySQL learning notes help document

View system help help contents mysql> help con...

Pure CSS3 to create page switching effect example code

The one I wrote before is too complicated, let’s ...

Two ways to implement HTML page click download file

1. Use the <a> tag to complete <a href=&...

JavaScript to achieve simple provincial and municipal linkage

This article shares the specific code for JavaScr...

Docker cleanup environment operation

Start cleaning carefully! List unused volumes doc...

React hooks pros and cons

Table of contents Preface advantage: shortcoming:...

A brief understanding of the relevant locks in MySQL

This article is mainly to take you to quickly und...

Detailed explanation of JavaScript Promise and Async/Await

Table of contents Overview Four examples Example ...

How to clean up Alibaba Cloud MySQL space

Today I received a disk warning notification from...

Detailed explanation of MySQL/Java server support for emoji and problem solving

This article describes the support and problem so...

Detailed steps to install Docker 1.8 on CentOS 7

Docker supports running on the following CentOS v...

How to use mysqldump to backup MySQL data

1. Introduction to mysqldump mysqldump is a logic...