React+ts实现二级联动效果

 更新时间:2022年04月26日 09:51:04   作者:博-轩  
这篇文章主要为大家详细介绍了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<Props, State>{
  LeftList: Button[]
  RightList: Content[]
  kaiguan: boolean
  right = createRef<HTMLDivElement>()
  left = createRef<HTMLDivElement>()
  LeftTex = createRef<HTMLDivElement>()
  // oTop: number | undefined
  viewHeight: number | undefined
  offHeight: number | undefined
  Lefttext = createRef<HTMLDivElement>()
  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<HTMLDivElement>) {
    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 (
      <div>
        <div className="about">
          <div className="scroll">
            <div className="box1" ref="box1"></div>
            <div className="box2" ref="box2"></div>
            <div className="scroll-con" ref="scroll-con">
              <div className="left"  ref={this.LeftTex}>
                <div className="left-con">
                  {footList.map((item, index) =>
                    <div onClick={this.Fn.bind(this, index)} ref={this.Lefttext} className={this.state.ButtonIndex === index ? "ac left-txt" : "left-txt"} key={item.id} >
                      {item.text}
                    </div>
                  )}
                </div>
              </div>
              <div className="right" ref={this.right} onScroll={this.FnScroll.bind(this)}>
                <div className="right-con">
                  <div
                    className="right-txt"
                    ref="right-txt">
                    {this.state.ContentList.map((item) =>
                      <div style={{ height: item.height }} className="right-title" key={item.id}>{item.text} </div>
                    )}
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
 
    )
 
  }
 
}
 
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 Native Flexbox布局(小结)

    浅谈React Native Flexbox布局(小结)

    这篇文章主要介绍了浅谈React Native Flexbox布局(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • react-native动态切换tab组件的方法

    react-native动态切换tab组件的方法

    在APP中免不了要使用tab组件,有的是tab切换,也有的是tab分类切换.这篇文章主要介绍了react-native动态切换tab组件的方法,非常具有实用价值,需要的朋友可以参考下
    2018-07-07
  • react拖拽组件react-sortable-hoc的使用

    react拖拽组件react-sortable-hoc的使用

    本文主要介绍了react拖拽组件react-sortable-hoc的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • React Render Props共享代码技术

    React Render Props共享代码技术

    render props是指一种在 React 组件之间使用一个值为函数的 prop 共享代码的技术。简单来说,给一个组件传入一个prop,这个props是一个函数,函数的作用是用来告诉这个组件需要渲染什么内容,那么这个prop就成为render prop
    2023-01-01
  • react+antd实现动态编辑表格数据

    react+antd实现动态编辑表格数据

    这篇文章主要为大家详细介绍了react+antd实现动态编辑表格数据,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • react ant Design手动设置表单的值操作

    react ant Design手动设置表单的值操作

    这篇文章主要介绍了react ant Design手动设置表单的值操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • react中使用css的7中方式(最全总结)

    react中使用css的7中方式(最全总结)

    这篇文章主要介绍了react中使用css的7中方式(最全总结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • 详解如何构建自己的react hooks

    详解如何构建自己的react hooks

    我们组的前端妹子在组内分享时谈到了 react 的钩子,趁此机会我也对我所理解的内容进行下总结,方便更多的同学了解。在 React 的 v16.8.0 版本里添加了 hooks 的这种新的 API,我们非常有必要了解下他的使用方法,并能够结合我们的业务编写几个自定义的 hooks。
    2021-05-05
  • 一文详解如何使用React监听网络状态

    一文详解如何使用React监听网络状态

    在现代Web应用程序中,网络连接是至关重要的,通过监听网络状态,我们可以为用户提供更好的体验,例如在断网时显示有关网络状态的信息,本文将介绍如何使用React监听网络状态的变化,并提供相应的代码示例
    2023-06-06
  • react dva实现的代码

    react dva实现的代码

    dva是一个基于redux和redux-saga的数据流方案,然后为了简化开发体验,dva额外内置了react-router,fetch,可以激烈为一个轻量级的应用框架,这篇文章主要介绍了react dva实现,需要的朋友可以参考下
    2021-11-11

最新评论