React实现二级联动(左右联动)

 更新时间:2021年09月10日 10:34:23   作者:小周同学:  
这篇文章主要为大家详细介绍了React实现二级联动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了React实现二级联动的具体代码,供大家参考,具体内容如下

js代码

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

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

        // 添加左侧
        this.FnButtonList = []
        //添加右侧
        this.FnContentList = []
        // 开关
        this.ScrollBys = true
        // 在constructor中直接执行——>react更新时才会渲染——>componentDidMount时才能触发获取
        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
    }
    // 随机数
    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}`
            })
        }
    }

    // 渲染右侧内容
    FnSetContent(n) {
        let ContTop = 0;//第一个元素距离页面顶部的距离
        let Random = this.FnSetRandom(750, 1400)
        for (let i = 0; i < n; i++) {
            this.FnContentList.push({
                height: Random,
                id: `内容${i}`,
                text: `内容${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 */
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • React router动态加载组件之适配器模式的应用详解

    React router动态加载组件之适配器模式的应用详解

    这篇文章主要介绍了React router动态加载组件之适配器模式的应用 ,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-09-09
  • 基于webpack4.X从零搭建React脚手架的方法步骤

    基于webpack4.X从零搭建React脚手架的方法步骤

    这篇文章主要介绍了基于webpack4.X从零搭建React脚手架的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • React Hook中的useEffecfa函数的使用小结

    React Hook中的useEffecfa函数的使用小结

    React 会在组件更新和卸载的时候执行清除操作, 将上一次的监听取消掉, 只留下当前的监听,这篇文章主要介绍了React Hook useEffecfa函数的使用细节详解,需要的朋友可以参考下
    2022-11-11
  • React Fiber源码深入分析

    React Fiber源码深入分析

    Fiber 可以理解为一个执行单元,每次执行完一个执行单元,React Fiber就会检查还剩多少时间,如果没有时间则将控制权让出去,然后由浏览器执行渲染操作,这篇文章主要介绍了React Fiber架构原理剖析,需要的朋友可以参考下
    2022-11-11
  • 详解如何用webpack4从零开始构建react开发环境

    详解如何用webpack4从零开始构建react开发环境

    这篇文章主要介绍了详解如何用webpack4从零开始构建react开发环境,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • 学习ahooks useRequest并实现手写

    学习ahooks useRequest并实现手写

    这篇文章主要为大家介绍了学习ahooks useRequest并实现手写示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • React实现轮播图效果

    React实现轮播图效果

    这篇文章主要为大家详细介绍了React实现轮播图效果,使用react-slick组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • React Native 通告消息竖向轮播组件的封装

    React Native 通告消息竖向轮播组件的封装

    这篇文章主要介绍了React Native 通告消息竖向轮播组件的封装,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • React中useTransition钩子函数的使用详解

    React中useTransition钩子函数的使用详解

    React 18的推出标志着React并发特性的正式到来,其中useTransition钩子函数是一个重要的新增功能,下面我们就来学习一下useTransition钩子函数的具体使用吧
    2024-02-02
  • React Fiber原理深入分析

    React Fiber原理深入分析

    Fiber可以理解为一个执行单元,每次执行完一个执行单元,React Fiber就会检查还剩多少时间,如果没有时间则将控制权让出去,然后由浏览器执行渲染操作,这篇文章主要介绍了React Fiber架构原理剖析,需要的朋友可以参考下<BR>
    2023-01-01

最新评论