JavaScript to achieve floor effect

JavaScript to achieve floor effect

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:
  • Detailed explanation of the process of creating floor navigation effects with JavaScript
  • 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

<<:  20 CSS coding tips to make you more efficient (sorted)

>>:  4 Practical Tips for Web Page Design

Recommend

Example of how to adapt the Vue project to the large screen

A brief analysis of rem First of all, rem is a CS...

Vue3 Documentation Quick Start

Table of contents 1. Setup 1. The first parameter...

Native JS to achieve drag photo wall

This article shares with you a draggable photo wa...

What is jQuery used for? jQuery is actually a js framework

Introduction to jQuery The jQuery library can be ...

Javascript scope and closure details

Table of contents 1. Scope 2. Scope Chain 3. Lexi...

Web page layout should consider IE6 compatibility issues

The figure below shows the browser viewing rate i...

Detailed tutorial on docker-compose deployment and configuration of Jenkins

Docker-compose deployment configuration jenkins 1...

Detailed explanation of several solutions for JavaScript interruption requests

Table of contents 1 Promise Interrupt the call ch...

JavaScript immediate execution function usage analysis

We know that in general, a function must be calle...

20 JS abbreviation skills to improve work efficiency

Table of contents When declaring multiple variabl...