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

Why do we need Map when we already have Object in JavaScript?

Table of contents 1. Don’t treat objects as Maps ...

MySQL Null can cause 5 problems (all fatal)

Table of contents 1. Count data is lost Solution ...

Summary of the use of html meta tags (recommended)

Meta tag function The META tag is a key tag in th...

How to use VIM editor in Linux

As a powerful editor with rich options, Vim is lo...

Detailed tutorial on installing JDK1.8 on Linux

1. Cleaning before installation rpm -qa | grep jd...

A brief discussion on macrotasks and microtasks in js

Table of contents 1. About JavaScript 2. JavaScri...

Solution to MySQL remote connection failure

I have encountered the problem that MySQL can con...

Solution to the timeout problem when installing docker-compose with PIP

1: Installation command pip install docker-compos...

Docker /var/lib/docker/aufs/mnt directory cleaning method

The company's service uses docker, and the di...

How to open external network access rights for mysql

As shown below: Mainly execute authorization comm...

Vue implements automatic jump to login page when token expires

The project was tested these days, and the tester...

Detailed explanation of Angular component projection

Table of contents Overview 1. Simple Example 1. U...

VMware virtualization kvm installation and deployment tutorial summary

Virtualization 1. Environment Centos7.3 Disable s...