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

React實現(xiàn)二級聯(lián)動(左右聯(lián)動)

 更新時間:2021年09月10日 10:34:23   作者:小周同學:  
這篇文章主要為大家詳細介紹了React實現(xiàn)二級聯(lián)動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了React實現(xiàn)二級聯(lián)動的具體代碼,供大家參考,具體內(nèi)容如下

js代碼

import { Component } from 'react'
import './linkage.less'

class Linkage extends Component {
    constructor(...args) {
        super(...args)

        // 添加左側
        this.FnButtonList = []
        //添加右側
        this.FnContentList = []
        // 開關
        this.ScrollBys = true
        // 在constructor中直接執(zhí)行——>react更新時才會渲染——>componentDidMount時才能觸發(fā)獲取
        this.init()

    }
    init() {
        this.FnSetButton(20)
        // 右側的渲染
        this.FnSetContent(20)
        this.state = {
            ButtonList: this.FnButtonList,
            ContentList: this.FnContentList,
            // 下標
            ButtonListIndex: 0,
        }


    }
    componentDidMount() {
        this.EveryHeight = this.refs['linkage-button-list'].children[0].offsetHeight
    }
    // 隨機數(shù)
    FnSetRandom(m, n) {
        return parseInt(Math.random() * (m - n) + n);
    }
    // 渲染左側的按鈕
    FnSetButton(n) {
        for (var i = 0; i < n; i++) {
            this.FnButtonList.push({
                id: `按鈕${i}`,
                text: `按鈕${i}`
            })
        }
    }

    // 渲染右側內(nèi)容
    FnSetContent(n) {
        let ContTop = 0;//第一個元素距離頁面頂部的距離
        let Random = this.FnSetRandom(750, 1400)
        for (let i = 0; i < n; i++) {
            this.FnContentList.push({
                height: Random,
                id: `內(nèi)容${i}`,
                text: `內(nèi)容${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;
        }
    }
    // 點擊
    FnButtonTab(index) {
        this.ScrollBys = false
        this.setState({
            ButtonListIndex: index
        })
        this.refs["linkage-content"].scrollTop = this.state.ContentList[index].top;

        //點擊居中
        this.Fncurrn(index)
    }

    //右邊滾動左邊
    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
                ) {
                    //盒子滾動的距離如果大于右邊盒子里的元素距離頁面頂部的距離
                    n = i;
                }
            }
            this.setState({
                ButtonListIndex: n
            })

            if (Math.abs(n - this.state.ButtonListIndex) === 1) {

                this.setState({
                    ButtonListIndex: n
                })
                //滾動居中

                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文件

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 */
  }

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • React router動態(tài)加載組件之適配器模式的應用詳解

    React router動態(tài)加載組件之適配器模式的應用詳解

    這篇文章主要介紹了React router動態(tài)加載組件之適配器模式的應用 ,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-09-09
  • 基于webpack4.X從零搭建React腳手架的方法步驟

    基于webpack4.X從零搭建React腳手架的方法步驟

    這篇文章主要介紹了基于webpack4.X從零搭建React腳手架的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • React?Hook中的useEffecfa函數(shù)的使用小結

    React?Hook中的useEffecfa函數(shù)的使用小結

    React 會在組件更新和卸載的時候執(zhí)行清除操作, 將上一次的監(jiān)聽取消掉, 只留下當前的監(jiān)聽,這篇文章主要介紹了React?Hook?useEffecfa函數(shù)的使用細節(jié)詳解,需要的朋友可以參考下
    2022-11-11
  • React Fiber源碼深入分析

    React Fiber源碼深入分析

    Fiber 可以理解為一個執(zhí)行單元,每次執(zhí)行完一個執(zhí)行單元,React Fiber就會檢查還剩多少時間,如果沒有時間則將控制權讓出去,然后由瀏覽器執(zhí)行渲染操作,這篇文章主要介紹了React Fiber架構原理剖析,需要的朋友可以參考下
    2022-11-11
  • 詳解如何用webpack4從零開始構建react開發(fā)環(huán)境

    詳解如何用webpack4從零開始構建react開發(fā)環(huán)境

    這篇文章主要介紹了詳解如何用webpack4從零開始構建react開發(fā)環(huán)境,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • 學習ahooks useRequest并實現(xiàn)手寫

    學習ahooks useRequest并實現(xiàn)手寫

    這篇文章主要為大家介紹了學習ahooks useRequest并實現(xiàn)手寫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • React實現(xiàn)輪播圖效果

    React實現(xiàn)輪播圖效果

    這篇文章主要為大家詳細介紹了React實現(xiàn)輪播圖效果,使用react-slick組件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • React Native 通告消息豎向輪播組件的封裝

    React Native 通告消息豎向輪播組件的封裝

    這篇文章主要介紹了React Native 通告消息豎向輪播組件的封裝,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • React中useTransition鉤子函數(shù)的使用詳解

    React中useTransition鉤子函數(shù)的使用詳解

    React?18的推出標志著React并發(fā)特性的正式到來,其中useTransition鉤子函數(shù)是一個重要的新增功能,下面我們就來學習一下useTransition鉤子函數(shù)的具體使用吧
    2024-02-02
  • React?Fiber原理深入分析

    React?Fiber原理深入分析

    Fiber可以理解為一個執(zhí)行單元,每次執(zhí)行完一個執(zhí)行單元,React?Fiber就會檢查還剩多少時間,如果沒有時間則將控制權讓出去,然后由瀏覽器執(zhí)行渲染操作,這篇文章主要介紹了React?Fiber架構原理剖析,需要的朋友可以參考下<BR>
    2023-01-01

最新評論

湟源县| 陇西县| 达孜县| 舒兰市| 涟水县| 鞍山市| 阿城市| 梅河口市| 大宁县| 从江县| 石台县| 绥芬河市| 扎兰屯市| 天祝| 白沙| 屯昌县| 成都市| 商城县| 南靖县| 静安区| 普兰县| 攀枝花市| 遂川县| 舟山市| 吉木萨尔县| 昌邑市| 宁河县| 大关县| 来宾市| 巨野县| 枣庄市| 拉萨市| 沧州市| 吉隆县| 平原县| 泸州市| 汾阳市| 平南县| 光泽县| 札达县| 青浦区|