React realizes secondary linkage effect (staircase effect)

React realizes secondary linkage effect (staircase effect)

This article shares the specific code of React to achieve the secondary linkage effect for your reference. The specific content is as follows

Imitate Ele.me to achieve a secondary linkage effect;

import "../css/Leftrightlinkage.less";
import React, { Component } from "react";
 
export default class Leftrightlinkage extends Component {
  constructor(...args) {
    super(...args);
    this.state = {
      list: [
        { id: 1, title: "List 1" },
        { id: 2, title: "List 2" },
        { id: 3, title: "List 3" },
        { id: 4, title: "List 4" },
        { id: 5, title: "List 5" },
        { id: 6, title: "List 6" },
        { id: 7, title: "List 7" },
        { id: 8, title: "List 8" },
        { id: 9, title: "List 9" },
        { id: 10, title: "List 10" },
      ],
      LeftList: [
        { id: 1, title: "List 1", height: 600 },
        { id: 2, title: "List 2", height: 600 },
        { id: 3, title: "List 3", height: 600 },
        { id: 4, title: "List 4", height: 600 },
        { id: 5, title: "List 5", height: 600 },
        { id: 6, title: "List 6", height: 600 },
        { id: 7, title: "List 7", height: 600 },
        { id: 8, title: "List 8", height: 600 },
        { id: 9, title: "List 9", height: 600 },
        { id: 10, title: "List 10", height: 600 },
      ],
      curr: 0, //Store subscript};
 
    // Add one by default because the first scrollTop value is 0
    this.LeftHeight = [0];
    //Scroll switch this.Swich = true;
  }
 
  // Rendering is complete and the distance between each list and the top is obtained componentDidMount() {
    // Defined as 0, each time you loop, the sum is the distance from the box to the top this.Height = 0;
    // Loop to get the height of each for (var i = 0; i < this.state.LeftList.length - 1; i++) {
      this.Height += this.state.LeftList[i].height;
      this.LeftHeight.push(this.Height);
    }
  }
  // Click on the left list to get the index FnTable(index) {
    // When clicking, set the scroll event on the right to false
    this.Swich = false;
    //Store index this.setState({
      curr: index,
    });
    //According to the subscript, take out the scrollTop value corresponding to the subscript in the array and let the scrollTop on the right be the value taken out from the array this.refs["leftItem"].scrollTop = this.LeftHeight[index];
  }
  FnScroll() {
    // Listen for scrolling this.scrollTop = this.refs["leftItem"].scrollTop;
 
    // Here we use a switch to determine whether to execute if (this.Swich) {
      //Store subscript let num = 0;
      // Loop through the array to retrieve the values ​​for (var i = 0; i < this.LeftHeight.length - 1; i++) {
        if (this.scrollTop >= this.LeftHeight[i]) {
          num = i;
        }
      }
      //Store index this.setState({
        curr: num,
      });
    }
    // Check if the scroll value is equal to the value in the array and the switch is true
    if (this.scrollTop == this.LeftHeight[this.state.curr]) {
      setTimeout(() => {
        this.Swich = true;
      });
    }
  }
 
  render() {
    return (
      <div className="box">
        <div className="scroll">
          <div className="list-left">
            {this.state.list.map((item, index) => (
              <div
                className="left-item"
                ref="scrollLeft"
                className={this.state.curr === index ? "active" : "left-item"}
                key={item.id}
                onClick={this.FnTable.bind(this, index)}
              >
                {item.title}
              </div>
            ))}
          </div>
          <div
            className="list-right"
            ref="leftItem"
            onScroll={this.FnScroll.bind(this)}
          >
            {this.state.LeftList.map((item) => (
              <div
                className="right-item"
                key={item.id}
                style={{ height: item.height }}
              >
                <div className="item-title">{item.title}</div>
              </div>
            ))}
          </div>
        </div>
      </div>
    );
  }
}

CSS style, the file format is Less format

 .box {
     width: 100vw;
     height: 100vh;
 
     .scroll {
         width: 100vw;
         height: 100vh;
         display: flex;
 
         .list-left {
             width: 200px;
             height: 100vh;
             background: rgb(151, 151, 151);
 
             .left-item {
                 height: 120px;
                 text-align: center;
                 line-height: 120px;
                 color: #ffffff;
                 font-size: 36px;
                 border: 3px solid #ffffff;
                 box-sizing: border-box;
             }
 
             .active {
                 height: 120px;
                 text-align: center;
                 line-height: 120px;
                 color: #ffffff;
                 font-size: 36px;
                 border: 3px solid #ffffff;
                 background-color: #f100d9;
                 box-sizing: border-box;
             }
         }
 
         .list-right {
             width: 100vw;
             height: 100vh;
             background-color: #15ff00;
             overflow: scroll;
 
             .right-item {
                 height: 400px;
                 border: 5px solid #0040ff;
                 font-size: 40px;
                 color: #ffffff;
                 box-sizing: border-box;
             }
         }
     }
 
 }

Effect picture:

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:
  • React's method of realizing secondary linkage
  • React realizes secondary linkage (left and right linkage)
  • React+ts realizes secondary linkage effect

<<:  Detailed explanation of Linux dynamic library generation and usage guide

>>:  Detailed explanation of the download process of the mysql-connector-java.jar package

Recommend

How to show or hide common icons on the desktop in Windows Server 2012

Windows Server 2012 and Windows Server 2008 diffe...

How to create a flame effect using CSS

The main text starts below. 123WORDPRESS.COM Down...

Mysql uses stored procedures to quickly add millions of data sample code

Preface In order to reflect the difference betwee...

Search optimization knowledge to pay attention to in web design

1. Link layout of the new site homepage 1. The loc...

Vue.js performance optimization N tips (worth collecting)

Table of contents Functionalcomponents Childcompo...

A brief discussion on whether CSS animation will be blocked by JS

The animation part of CSS will be blocked by JS, ...

Unity connects to MySQL and reads table data implementation code

The table is as follows: Code when Unity reads an...

Detailed explanation of common MySQL operation commands in Linux terminal

Serve: # chkconfig --list List all system service...

In-depth explanation of the maximum value of int in MySQL

Introduction I will write about the problem I saw...

What magical uses does CSS filter have

background Basic Concepts CSS filter property app...

How to install MySQL Community Server 5.6.39

This article records the detailed tutorial of MyS...

How to generate mysql primary key id (self-increment, unique and irregular)

Table of contents 1. Use the uuid function to gen...

Unicode signature BOM (Byte Order Mark) issue for UTF-8 files

I recently encountered a strange thing when debug...