React和ts如何实现二级联动效果
发表于:2025-11-15 作者:千家信息网编辑
千家信息网最后更新 2025年11月15日,这篇文章主要介绍React和ts如何实现二级联动效果,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!.tsx文件import { Component, createRef} fr
千家信息网最后更新 2025年11月15日React和ts如何实现二级联动效果
这篇文章主要介绍React和ts如何实现二级联动效果,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
.tsx文件
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: number | 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文件
.scroll { 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; } } }}最后把自己定义的文件夹添加到路由里即可
效果图如下
以上是"React和ts如何实现二级联动效果"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
效果
文件
内容
篇文章
价值
兴趣
小伙
小伙伴
按钮
效果图
文件夹
更多
标题
知识
行业
资讯
资讯频道
路由
频道
参考
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
智慧医院网络安全口号
北大法意法律数据库官网
数据库写入数据怎么设置回滚
网络技术与sdn
网络安全的优势
专用有限元软件开发
业务数据库相关技术
虚拟服务器远程管理
成都忠华盛世互联网科技有限公司
浪潮服务器一长
基岩版连java版服务器
罗定定制软件开发
web服务器ssl数据安全
信息与网络安全案例分析
山东电脑软件开发靠谱吗
j2ee数据库编码
某单位网络安全方案项目设计
设计模式在软件开发中的意义
网络安全等级评定办法
qq 接收邮件服务器
vb sqlite数据库
关于提取电子数据库
捕获视频软件开发
剑灵与服务器断开连接
河北网络技术分类产品介绍
系统数据库包括什么
阿里云 网络安全 招聘
龙族幻想如何开启创建服务器
中国网络安全企业50
定远工业软件开发技术怎么样