Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of two-stage linkage between React and ts

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces React and ts how to achieve secondary linkage effect, the article is very detailed, has a certain reference value, interested friends must read it!

.tsx file

Import {Component CreateRef} from 'react' import'. / index.less' interface State {top: any ButtonList: Button [] ContentList: Content [] ButtonIndex: number} interface Button {id: string text: string} interface Content {id: string text: string height: number top: number} interface Props {} class Stairs extends Component {LeftList: Button [] RightList: Content [] kaiguan: boolean right = createRef () left = createRef () LeftTex = createRef () / oTop: number | undefined viewHeight: undefined viewHeight | Undefined offHeight: number | undefined Lefttext = createRef () top: number | undefined oTop: number | undefined constructor (props: Props) {super (props) this.state = {ButtonList: [] ContentList: [], ButtonIndex: 0, top: 0} this.LeftList = [] this.RightList = [] this.kaiguan = true this.oTop = 0} componentDidMount () {this.BtnList (20) this.ConList (20) this.setState ({ButtonList: this.LeftList, ContentList: this.RightList})} getRandom (m: number N: number): number {return parseInt (`${Math.random () * (m-n) + n}`) } BtnList (n: number) {for (let I = 0; I)

< n; i++) { this.LeftList.push({ id: `a${i}`, text: `按钮${i}`, }); } } ConList(n: number) { let ConTop = 0; for (let i = 0; i < n; i++) { let RandomHeight = this.getRandom(736, 1400); this.RightList.push({ id: `b${i}`, text: `标题${i}`, height: RandomHeight, top: ConTop, }); ConTop += RandomHeight; } } FnScroll() { // console.log(11) if (this.right.current) { this.oTop = this.right.current.scrollTop; if (this.kaiguan) { // console.log(111) let count = 0 for (var i = 0; i < this.state.ContentList.length; i++) { if (this.oTop >

= this.state.ContentList [I] .top) {count = I} this.setState ({ButtonIndex: count})} / / console.log (ButtonIndex,count)}} / / eslint-disable-next-line if (this.oTop = = this.state.ContentList [this.state.ButtonIndex] .top) {this.kaiguan = true }} Fn (index: any, ev: React.MouseEvent) {this.viewHeight = document.documentElement.clientHeight / 2 let target = ev.target as HTMLDivElement this.offHeight = target.offsetTop / / console.log (this.offHeight) if (this.offHeight > this.viewHeight) {if (this.LeftTex.current) {this.LeftTex.current.scrollTo ({top: this.offHeight-this.viewHeight-target.clientHeight / 2 Behavior: "smooth",})} / / console.log (this.LeftTex.current)} / / console.log (this.offHeight-this.viewHeight-target.clientHeight / 2) this.kaiguan = false / / this.offHeight = ev.target.offsetTop / / console.log (ev.target) if (this.right.current) {this.right.current.scroll ({top: this.RightList [index] .top, behavior: "smooth",}) } this.setState ({ButtonIndex: index})} ButtonIndex (index: number) {if (index > = 3) {if (this.left.current & & this.Lefttext.current) {this.left.current.scrollTop = (index-3) * this.Lefttext.current.offsetHeight;}} if (index

< 3) { if (this.left.current) { this.left.current.scrollTop = 0; } } this.setState({ ButtonIndex: index }) } render() { let footList = this.state.ButtonList return ( {footList.map((item, index) =>

{item.text})} {this.state.ContentList.map ((item) = > {item.text})} )} export default Stairs

.less file

.box {width: 100vw; height: 100vh; overflow-y: scroll; .box1 {height: 300px; background: # 000; width: 100%;} .box2 {height: 200px; background: tomato; width: 100%;} .box3 {position:-webkit-sticky; position: sticky; top: 0; height: 100px; background: palevioletred; z-index: 999; width: 100% } .scroll-con {width: 100vW; height: 100vh; position:-webkit-sticky; position: sticky; top: 100px; display: flex; .left, .right {height: 100vh; overflow-y: scroll;} .left {width: 20vW; .left-txt {width: 20vW; height: 100px; text-align: center Line-height: 100px; background: red;}. Left-txt.ac {background: lightcoral; z-index: 999;}} .right {width: 80vw; .right-title {width: 100%; height: 5vh; background: darkblue; color: aqua; line-height: 5vh;}

Finally, add the self-defined folder to the route.

The effect picture is as follows

The above is all the content of this article "how to achieve secondary linkage effect between React and ts". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report