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

Four ways to create objects in JS

Table of contents 1. Create objects by literal va...

js to realize automatic lock screen function

1. Usage scenarios There is such a requirement, s...

10 ways to view compressed file contents in Linux (summary)

Generally speaking, when we view the contents of ...

CSS menu button animation

To write a drop-down menu, click the button. The ...

Detailed explanation of global parameter persistence in MySQL 8 new features

Table of contents Preface Global parameter persis...

Javascript asynchronous programming: Do you really understand Promise?

Table of contents Preface Basic Usage grammar Err...

Detailed tutorial for downloading, installing and configuring MySQL 5.7.27

Table of contents 1. Download steps 2. Configure ...

JavaScript implements password box input verification

Sometimes it is necessary to perform simple verif...

A brief talk about the diff algorithm in Vue

Table of contents Overview Virtual Dom principle ...

Detailed explanation of MySQL basic operations (Part 2)

Preface This article contains 1. Several major co...

How to use Nginx to handle cross-domain Vue development environment

1. Demand The local test domain name is the same ...

Ubuntu16.04 builds php5.6 web server environment

Ubuntu 16.04 installs the PHP7.0 environment by d...

How to control the proportion of Flex child elements on the main axis

background Flex layout achieves alignment and spa...

Using better-scroll component in Vue to realize horizontal scrolling function

About Recently, in the process of learning Vue, I...

Docker deploys nginx and mounts folders and file operations

During this period of time, I was studying docker...