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

Example of using Vue built-in component keep-alive

Table of contents 1. Usage of keep-alive Example ...

Vue Basics Listener Detailed Explanation

Table of contents What is a listener in vue Usage...

How to deploy Vue project using Docker image + nginx

1. Packaging Vue project Enter the following name...

Solve the problem of docker log mounting

The key is that the local server does not have wr...

Solution to the ineffective global style of the mini program custom component

Table of contents Too long to read Component styl...

Using MySQL database in docker to achieve LAN access

1. Get the mysql image docker pull mysql:5.6 Note...

MySQL SQL Optimization Tutorial: IN and RANGE Queries

First, let's talk about the in() query. It is...

Linux Centos8 Create CA Certificate Tutorial

Install Required Files Yum install openssl-* -y C...

Detailed explanation of transaction isolation levels in MySql study notes

background When we talk about transactions, every...

MySQL establishes efficient index example analysis

This article uses examples to describe how to cre...

Detailed explanation of MySQL date string timestamp conversion

The conversion between time, string and timestamp...

Vue component library ElementUI realizes the paging effect of table list

ElementUI implements the table list paging effect...

Detailed explanation of three ways to connect Docker containers to each other

There are three ways to interconnect and communic...

Tips for using top command in Linux

First, let me introduce the meaning of some field...