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
The various HTML documents of the website are con...
Log in to your account export DOCKER_REGISTRY=reg...
This article uses examples to illustrate the usag...
In fact, it is not difficult to build an Apache c...
Before understanding this problem, let's firs...
Copy code The code is as follows: <!DOCTYPE ht...
Table of contents Implementation ideas There are ...
This article uses an example to illustrate how to...
Table of contents Linux-Use MyCat to implement My...
After entering yum in linux, the prompt: -bash: /...
This article uses examples to describe the operat...
Table of contents Purpose npm init and package.js...
MySQL handles GROUP BY and DISTINCT queries simil...
MySQL supports three types of comments: 1. From t...
inline-flex is the same as inline-block. It is a ...