React realizes secondary linkage (left and right linkage)

React realizes secondary linkage (left and right linkage)

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

js code

import { Component } from 'react'
import './linkage.less'

class Linkage extends Component {
    constructor(...args) {
        super(...args)

        // Add the left side this.FnButtonList = []
        //Add the right side this.FnContentList = []
        // Switch this.ScrollBys = true
        // Execute directly in the constructor --> React will be rendered only when it is updated --> componentDidMount will trigger the acquisition of this.init()

    }
    init() {
        this.FnSetButton(20)
        // Rendering on the right this.FnSetContent(20)
        this.state = {
            ButtonList: this.FnButtonList,
            ContentList: this.FnContentList,
            //Subscript ButtonListIndex: 0,
        }


    }
    componentDidMount() {
        this.EveryHeight = this.refs['linkage-button-list'].children[0].offsetHeight
    }
    // Random number FnSetRandom(m, n) {
        return parseInt(Math.random() * (m - n) + n);
    }
    // Render the button on the left FnSetButton(n) {
        for (var i = 0; i < n; i++) {
            this.FnButtonList.push({
                id: `button${i}`,
                text: `Button${i}`
            })
        }
    }

    // Render the right content FnSetContent(n) {
        let ContTop = 0; //The distance between the first element and the top of the page let Random = this.FnSetRandom(750, 1400)
        for (let i = 0; i < n; i++) {
            this.FnContentList.push({
                height: Random,
                id: `content${i}`,
                text: `content${i}`,
                top: ContTop,
            });
            ContTop += Random;
        }
    }

    Fncurrn(index) {
        if (index > 3) {
            this.refs["linkage-button"].scrollTop = (index - 3) * this.EveryHeight;

        }
        if (index <= 3) {
            this.refs["linkage-button"].scrollTop = 0;
        }
    }
    // Click FnButtonTab(index) {
        this.ScrollBys = false
        this.setState({
            ButtonListIndex: index
        })
        this.refs["linkage-content"].scrollTop = this.state.ContentList[index].top;

        //Click to center this.Fncurrn(index)
    }

    //Scroll the right side and the left side FnScroll(ev) {
        this.ContTop = ev.target.scrollTop
        if (this.ScrollBys) {
            let n = 0

            for (let i = 0; i < this.state.ContentList.length; i++) {
                if (
                    this.refs["linkage-content"].scrollTop >= this.state.ContentList[i].top
                ) {
                    //If the scroll distance of the box is greater than the distance between the element in the right box and the top of the page n = i;
                }
            }
            this.setState({
                ButtonListIndex: n
            })

            if (Math.abs(n - this.state.ButtonListIndex) === 1) {

                this.setState({
                    ButtonListIndex: n
                })
                //Scroll to the center this.Fncurrn(n)

            }
        }


        if (this.ContTop == this.state.ContentList[this.state.ButtonListIndex].top) {
            this.ScrollBys = true
        }

    }

    render() {
        return (
            <div className="linkage">
                <div className="linkage-button" ref="linkage-button">
                    <div className="linkage-button-list" ref="linkage-button-list">
                        {this.state.ButtonList.map((item, index) => <div
                            key={item.id}
                            className={this.state.ButtonListIndex == index ? 'linkage-button-item ac' : 'linkage-button-item'}
                            onClick={this.FnButtonTab.bind(this, index)}
                        >
                            {item.text}
                        </div>)}
                    </div>
                </div>
                <div className="linkage-content" ref="linkage-content" onScroll={this.FnScroll.bind(this)}>
                    <div className="linkage-content-list">
                        {this.state.ContentList.map((item) => <div
                            className="linkage-content-item"
                            key={item.id}
                            style={{ height: item.height + 'px' }}
                        >
                            <div className="linkage-content-title"> {item.text}</div>
                        </div>)}
                    </div>
                </div>
            </div>
        )
    }
}
export default Linkage

css file

body {
    margin: 0;
  }
  .linkage {
    width: 100vw;
    height: 100vh;
    display: flex;
    .linkage-button {
      width: 20vw;
      height: 100vh;
      background: chocolate;
      text-align: center;
      font-size: 40px;
      color: #fff;
      overflow: scroll;
      scroll-behavior: smooth;
      .linkage-button-list {
        width: 20vw;
        .linkage-button-item.ac {
          background: lightblue;
        }
        .linkage-button-item {
          width: 20vw;
          height: 10vh;
          line-height: 10vh;
        }
      }
    }
    .linkage-content {
      width: 80vw;
      height: 100vh;
      scroll-behavior: smooth;
      overflow: scroll;
      .linkage-content-list {
        .linkage-content-item {
          width: 80vw;
          height: 100vh;
          .linkage-content-title {
            height: 6vh;
            line-height: 6vh;
            width: 80vw;
            text-align: center;
            background:chartreuse;
            color: #fff;
            font-size: 30px;
          }
        }
      }
    }
  }
  .linkage-button::-webkit-scrollbar {
    display: none; /* Chrome Safari */
  }
  .linkage-content::-webkit-scrollbar {
    display: none; /* Chrome Safari */
  }

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 effect (staircase effect)
  • React+ts realizes secondary linkage effect

<<:  Solve the problem that the MySQL database crashes unexpectedly, causing the table data file to be damaged and unable to start

>>:  Analysis of the process of simply deploying nginx in Docker container

Recommend

Tutorial on using the hyperlink tag in HTML

The various HTML documents of the website are con...

Summary of commonly used commands for docker competition submission

Log in to your account export DOCKER_REGISTRY=reg...

A brief analysis of the usage of USING and HAVING in MySQL

This article uses examples to illustrate the usag...

Analysis of the process of building a cluster environment with Apache and Tomcat

In fact, it is not difficult to build an Apache c...

What are the advantages of using B+ tree index in MySQL?

Before understanding this problem, let's firs...

How to use MyCat to implement MySQL master-slave read-write separation in Linux

Table of contents Linux-Use MyCat to implement My...

Specific use of Node.js package manager npm

Table of contents Purpose npm init and package.js...

MySQL group query optimization method

MySQL handles GROUP BY and DISTINCT queries simil...

Introduction to MySQL statement comments

MySQL supports three types of comments: 1. From t...

Detailed explanation of the difference between flex and inline-flex in CSS

inline-flex is the same as inline-block. It is a ...