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:
|
>>: Analysis of the process of simply deploying nginx in Docker container
Table of contents 1. Create objects by literal va...
1. Usage scenarios There is such a requirement, s...
Generally speaking, when we view the contents of ...
To write a drop-down menu, click the button. The ...
Table of contents Preface Global parameter persis...
Table of contents Preface Basic Usage grammar Err...
Table of contents 1. Download steps 2. Configure ...
Sometimes it is necessary to perform simple verif...
Table of contents Overview Virtual Dom principle ...
Preface This article contains 1. Several major co...
1. Demand The local test domain name is the same ...
Ubuntu 16.04 installs the PHP7.0 environment by d...
background Flex layout achieves alignment and spa...
About Recently, in the process of learning Vue, I...
During this period of time, I was studying docker...