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

Detailed explanation of how to introduce custom fonts (font-face) in CSS

Why did I use this? It all started with the makin...

JavaScript Basics Objects

Table of contents 1. Object 1.1 What is an object...

How to deploy FastDFS in Docker

Install fastdfs on Docker Mount directory -v /e/f...

Install Mininet from source code on Ubuntu 16.04

Mininet Mininet is a lightweight software defined...

14 techniques for high-performance websites

Original : http://developer.yahoo.com/performance...

Steps for Docker to build its own local image repository

1. Environment and preparation 1. Ubuntu 14.04 2....

Essential knowledge for web development interviews and written tests (must read)

The difference between inline elements and block-...

HTML reuse techniques

HTML reuse is a term that is rarely mentioned. Tod...

Mysql experiment: using explain to analyze the trend of indexes

Overview Indexing is a skill that must be mastere...

A brief understanding of the difference between MySQL union all and union

Union is a union operation on the data, excluding...

Optimized implementation of count() for large MySQL tables

The following is my judgment based on the data st...

WeChat Mini Programs Implement Star Rating

This article shares the specific code for WeChat ...

Using react+redux to implement counter function and problems encountered

Redux is a simple state manager. We will not trac...