最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

React+ts實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)效果

 更新時(shí)間:2022年04月26日 09:51:04   作者:博-軒  
這篇文章主要為大家詳細(xì)介紹了React+ts實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了React+ts實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下

.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: `標(biāo)題${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;
      }
    }
  }
}

最后把自己定義的文件夾添加到路由里即可

效果圖如下

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談React Native Flexbox布局(小結(jié))

    淺談React Native Flexbox布局(小結(jié))

    這篇文章主要介紹了淺談React Native Flexbox布局(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • react-native動(dòng)態(tài)切換tab組件的方法

    react-native動(dòng)態(tài)切換tab組件的方法

    在APP中免不了要使用tab組件,有的是tab切換,也有的是tab分類切換.這篇文章主要介紹了react-native動(dòng)態(tài)切換tab組件的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-07-07
  • react拖拽組件react-sortable-hoc的使用

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

    本文主要介紹了react拖拽組件react-sortable-hoc的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • React Render Props共享代碼技術(shù)

    React Render Props共享代碼技術(shù)

    render props是指一種在 React 組件之間使用一個(gè)值為函數(shù)的 prop 共享代碼的技術(shù)。簡(jiǎn)單來說,給一個(gè)組件傳入一個(gè)prop,這個(gè)props是一個(gè)函數(shù),函數(shù)的作用是用來告訴這個(gè)組件需要渲染什么內(nèi)容,那么這個(gè)prop就成為render prop
    2023-01-01
  • react+antd實(shí)現(xiàn)動(dòng)態(tài)編輯表格數(shù)據(jù)

    react+antd實(shí)現(xiàn)動(dòng)態(tài)編輯表格數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了react+antd實(shí)現(xiàn)動(dòng)態(tài)編輯表格數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • react ant Design手動(dòng)設(shè)置表單的值操作

    react ant Design手動(dòng)設(shè)置表單的值操作

    這篇文章主要介紹了react ant Design手動(dòng)設(shè)置表單的值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • react中使用css的7中方式(最全總結(jié))

    react中使用css的7中方式(最全總結(jié))

    這篇文章主要介紹了react中使用css的7中方式(最全總結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-02-02
  • 詳解如何構(gòu)建自己的react hooks

    詳解如何構(gòu)建自己的react hooks

    我們組的前端妹子在組內(nèi)分享時(shí)談到了 react 的鉤子,趁此機(jī)會(huì)我也對(duì)我所理解的內(nèi)容進(jìn)行下總結(jié),方便更多的同學(xué)了解。在 React 的 v16.8.0 版本里添加了 hooks 的這種新的 API,我們非常有必要了解下他的使用方法,并能夠結(jié)合我們的業(yè)務(wù)編寫幾個(gè)自定義的 hooks。
    2021-05-05
  • 一文詳解如何使用React監(jiān)聽網(wǎng)絡(luò)狀態(tài)

    一文詳解如何使用React監(jiān)聽網(wǎng)絡(luò)狀態(tài)

    在現(xiàn)代Web應(yīng)用程序中,網(wǎng)絡(luò)連接是至關(guān)重要的,通過監(jiān)聽網(wǎng)絡(luò)狀態(tài),我們可以為用戶提供更好的體驗(yàn),例如在斷網(wǎng)時(shí)顯示有關(guān)網(wǎng)絡(luò)狀態(tài)的信息,本文將介紹如何使用React監(jiān)聽網(wǎng)絡(luò)狀態(tài)的變化,并提供相應(yīng)的代碼示例
    2023-06-06
  • react dva實(shí)現(xiàn)的代碼

    react dva實(shí)現(xiàn)的代碼

    dva是一個(gè)基于redux和redux-saga的數(shù)據(jù)流方案,然后為了簡(jiǎn)化開發(fā)體驗(yàn),dva額外內(nèi)置了react-router,fetch,可以激烈為一個(gè)輕量級(jí)的應(yīng)用框架,這篇文章主要介紹了react dva實(shí)現(xiàn),需要的朋友可以參考下
    2021-11-11

最新評(píng)論

济源市| 余姚市| 太仆寺旗| 湛江市| 盘锦市| 临沂市| 内乡县| 临澧县| 岳池县| 科技| 黑水县| 邹城市| 探索| 松溪县| 亳州市| 车险| 湖南省| 同德县| 阿尔山市| 吉木乃县| 涟源市| 渭南市| 天津市| 安阳县| 南丰县| 鹤峰县| 仲巴县| 绥中县| 中阳县| 福海县| 湛江市| 海南省| 启东市| 乌兰县| 千阳县| 宁城县| 荃湾区| 凌云县| 咸丰县| 富平县| 蕉岭县|