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

More popular and creative dark background web design examples

Dark background style page design is very popular...

How to set a dotted border in html

Use CSS styles and HTML tag elements In order to ...

Use of Zabbix Api in Linux shell environment

You can call it directly in the Linux shell envir...

Node uses koa2 to implement a simple JWT authentication method

Introduction to JWT What is JWT The full name is ...

How to install mysql5.7.24 binary version on Centos 7 and how to solve it

MySQL binary installation method Download mysql h...

MySQL 8.0 New Features: Hash Join

The MySQL development team officially released th...

Implementing timed page refresh or redirect based on meta

Use meta to implement timed refresh or jump of th...

How to configure virtual user login in vsftpd

yum install vsftpd [root@localhost etc]# yum -y i...

Introduction to the use of the indeterminate property of the checkbox

When we use the folder properties dialog box in Wi...

Vue.js implements music player

This article shares the specific code of Vue.js t...

Solution to the problem that VC6.0 cannot be used when installed on WIN10

VC6.0 is indeed too old VC6.0 is a development to...

CSS hacks \9 and \0 may not work for hacking IE11\IE9\IE8

Every time I design a web page or a form, I am tr...

Pure CSS code to achieve flow and dynamic line effects

Ideas: An outer box sets the background; an inner...