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

Vue uses rules to implement form field validation

There are many ways to write and validate form fi...

Perfect solution to the problem of webpack packaging css background image path

Inside the style tag of the vue component, there ...

How to export mysql table structure to excel

The requirements are as follows Export the table ...

Table setting background image cannot be 100% displayed solution

The following situations were discovered during d...

Summary of knowledge points on using calculated properties in Vue

Computed properties Sometimes we put too much log...

MySQL Order By Multi-Field Sorting Rules Code Example

Say it in advance On a whim, I want to know what ...

Detailed steps for IDEA to integrate docker to achieve remote deployment

1. Enable remote access to the docker server Log ...

MySQL database optimization: index implementation principle and usage analysis

This article uses examples to illustrate the prin...

MySQL full-text fuzzy search MATCH AGAINST method example

MySQL 4.x and above provide full-text search supp...

MySQL data types full analysis

Data Type: The basic rules that define what data ...

Simple CSS text animation effect

Achieve results Implementation Code html <div ...

Implementation of 2D and 3D transformation in CSS3

CSS3 implements 2D plane transformation and visua...