亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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)

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

    }
    init() {
        this.FnSetButton(20)
        // 右側(cè)的渲染
        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);
    }
    // 渲染左側(cè)的按鈕
    FnSetButton(n) {
        for (var i = 0; i < n; i++) {
            this.FnButtonList.push({
                id: `按鈕${i}`,
                text: `按鈕${i}`
            })
        }
    }

    // 渲染右側(cè)內(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • create-react-app修改為多頁面支持的方法

    create-react-app修改為多頁面支持的方法

    本篇文章主要介紹了create-react-app修改為多頁面支持的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • antd form表單如何處理自定義組件

    antd form表單如何處理自定義組件

    這篇文章主要介紹了antd form表單如何處理自定義組件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • React前端DOM常見Hook封裝示例上

    React前端DOM常見Hook封裝示例上

    這篇文章主要為大家介紹了React前端DOM常見Hook封裝示例上篇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • React用法之高階組件的用法詳解

    React用法之高階組件的用法詳解

    高階組件也就是我們常說的HOC,是React中用于復用組件邏輯的一種高級技巧。這篇文章主要通過一些示例帶大家學習一下高階組件的使用,希望對大家有所幫助
    2023-04-04
  • React中useState的理解和使用案例

    React中useState的理解和使用案例

    Hook是React16.8的新增特性,它可以讓你在不編寫class的情況下使用state以及其他的React特性,本文中講解的useState就是React中的其中一個Hook,這篇文章主要給大家介紹了關(guān)于React中useState理解和使用的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • React大文件分片上傳原理及方案

    React大文件分片上傳原理及方案

    前端進行大文件分片上傳的方案幾乎都是利用Blob.prototype.slice方法對文件進行分片,用數(shù)組將每一個分片存起來,最后將分片發(fā)給后端,本文給大家介紹React大文件分片上傳方案,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • React中setState更新狀態(tài)的兩種寫法

    React中setState更新狀態(tài)的兩種寫法

    在?React?中,我們經(jīng)常需要更新組件的狀態(tài)(state),使用?setState?方法是一種常見的方式來實現(xiàn)狀態(tài)的更新,而在使用?setState?方法時,有兩種不同的寫法,即對象式和函數(shù)式,本文將介紹這兩種寫法的區(qū)別和使用場景,需要的朋友可以參考下
    2024-03-03
  • React實現(xiàn)評論的添加和刪除

    React實現(xiàn)評論的添加和刪除

    這篇文章主要為大家詳細介紹了React實現(xiàn)評論的添加和刪除,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • React?Virtual?DOM前端框架全面分析

    React?Virtual?DOM前端框架全面分析

    這篇文章主要為大家介紹了React?Virtual?DOM前端框架全面分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • React Native 如何獲取不同屏幕的像素密度

    React Native 如何獲取不同屏幕的像素密度

    這篇文章主要介紹了 React Native 如何 獲取不同屏幕的像素密度的相關(guān)資料,需要的朋友可以參考下
    2017-01-01

最新評論