react实现导航栏二级联动

 更新时间:2022年03月08日 09:54:12   作者:至_臻  
这篇文章主要为大家详细介绍了react实现导航栏二级联动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

效果图

js代码

import { Component } from "react";
import './scroll.less'
 
class Scroll extends Component {
    constructor(...args) {
        super(...args)
        this.state = {
            list: [
                { id: 1, title: '列表1' },
                { id: 2, title: '列表2' },
                { id: 3, title: '列表3' },
                { id: 4, title: '列表4' },
                { id: 5, title: '列表5' },
                { id: 6, title: '列表6' },
                { id: 7, title: '列表7' },
                { id: 8, title: '列表8' },
                { id: 9, title: '列表9' },
                { id: 10, title: '列表10' },
                { id: 11, title: '列表11' },
                { id: 12, title: '列表12' },
                { id: 13, title: '列表13' },
                { id: 14, title: '列表14' },
                { id: 15, title: '列表15' },
                { id: 16, title: '列表16' },
                { id: 17, title: '列表17' },
            ],
            LeftList: [
                { id: 1, title: '列表1', height: 800 },
                { id: 2, title: '列表2', height: 600 },
                { id: 3, title: '列表3', height: 500 },
                { id: 4, title: '列表4', height: 900 },
                { id: 5, title: '列表5', height: 450 },
                { id: 6, title: '列表6', height: 300 },
                { id: 7, title: '列表7', height: 900 },
                { id: 8, title: '列表8', height: 1010 },
                { id: 9, title: '列表9', height: 300 },
                { id: 10, title: '列表10', height: 600 },
                { id: 11, title: '列表11', height: 400 },
                { id: 12, title: '列表12', height: 760 },
                { id: 13, title: '列表13', height: 580 },
                { id: 14, title: '列表14', height: 630 },
                { id: 15, title: '列表15', height: 540 },
                { id: 16, title: '列表16', height: 983 },
                { id: 17, title: '列表17', height: 610 },
            ],
            curr: 0,//存储下标
        }
        // 默认添加一个 因为第一个的scrollTop值是0
        this.LeftHeight = [0]
        // 滚动的开关
        this.Swich = true
    }
    // 渲染完成获取每一个列表距离顶部的距离
    componentDidMount() {
        // 定义为0 每次就可以循环加起来就是盒子距离顶部的距离
        this.Height = 0
        // 循环获取每一个的高
        for (var i = 0; i < this.state.LeftList.length - 1; i++) {
            this.Height += this.state.LeftList[i].height
            // 将它添加到数组中
            this.LeftHeight.push(this.Height)
        }
    }
    //   点击左侧列表 点击获取下标
    fnTab(Ind) {
        // 点击的时候让右边的滚动事件为false
        this.Swich = false
        // 存储下标
        this.setState({
            curr: Ind
        })
        // 根据下标取出数组中对应下标的scrollTop值  就让右边的scrollTop为数组中取出的值
        this.refs['rightItem'].scrollTop = this.LeftHeight[Ind];
 
        // this.refs.scrollLeft.scrollTop = this.state.curr - 4 * 58.89
    }
    FnScroll() {
        // 监听滚动
        this.scrollTop = this.refs['rightItem'].scrollTop
        // 这边用开关判断是否执行
        if (this.Swich) {
            // 存放下标
            let num = 0
            // 循环取出数组中的数值
            for (var i = 0; i < this.LeftHeight.length - 1; i++) {
                if (this.scrollTop >= this.LeftHeight[i]) {
                    num = i
                }
            }
            // 存储下标
            this.setState({
                curr: num
            })
        }
        // 判断滚动的值和数组中的值相等 开关为true
        if (this.scrollTop == this.LeftHeight[this.state.curr]) {
            setTimeout(() => {
                this.Swich = true;
            });
        }
    }
    render() {
        return (
            <div className='box'>
                <div className='scroll'>
                    <div className='scroll-right' ref='scrollLeft'>
                        {this.state.list.map((item, index) => <div className='right-item' className={this.state.curr === index ? "active" : "right-item"} key={item.id} onClick={this.fnTab.bind(this, index)} >{item.title}</div>)}
                    </div>
                    <div className='scroll-left' ref='rightItem' onScroll={this.FnScroll.bind(this)}>
                        {this.state.LeftList.map((item) => <div className='left-item' key={item.id} style={{ height: item.height }}><div className='item-title'>{item.title}</div></div>)}
                    </div>
 
                </div>
            </div>
        )
    }
}
export default Scroll

less代码

* {
    margin: 0;
    padding: 0;
}
 
.box {
    width: 100vw;
    height: 100vh;
    background: red;
 
    .scroll {
        width: 100vw;
        height: 100vh;
        display: flex;
 
        // 右边列表
        .scroll-right {
            width: 25vw;
            background: aqua;
            font-size: 28px;
            height: 100vh;
            overflow-y: auto;
 
            .right-item {
                width: 25vw;
                height: 80px;
                text-align: center;
                line-height: 80px;
                border-bottom: 1px solid #ccc;
            }
 
            .active {
                height: 80px;
                text-align: center;
                line-height: 80px;
                background: #0f0;
            }
        }
 
        // 左边内容
        .scroll-left {
            width: 75vw;
            height: 100vh;
            overflow-y: auto;
            //滚动的更加丝滑
            scroll-behavior: smooth;
 
            .left-item {
                width: 75vw;
                font-size: 30px;
                text-align: center;
 
 
                .item-title {
                    height: 30px;
                    background: #ccc;
                }
            }
        }
    }
}

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

相关文章

  • 详解React的回调渲染模式

    详解React的回调渲染模式

    这篇文章主要介绍了详解React的回调渲染模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • React Native 通告消息竖向轮播组件的封装

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

    这篇文章主要介绍了React Native 通告消息竖向轮播组件的封装,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • react表单受控的实现方案

    react表单受控的实现方案

    数据的受控控制一直是react里的一个痛点,当我想要实现一个输入框的受控控制时,我需要定义一个onChange和value,手动去实现数据的绑定,本文小编给大家介绍了react表单受控的实现方案,需要的朋友可以参考下
    2023-12-12
  • React快速入门教程

    React快速入门教程

    本文主要介绍了React的相关知识,具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • 详解在React.js中使用PureComponent的重要性和使用方式

    详解在React.js中使用PureComponent的重要性和使用方式

    这篇文章主要介绍了详解在React.js中使用PureComponent的重要性和使用方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • React中setState/useState的使用方法详细介绍

    React中setState/useState的使用方法详细介绍

    这篇文章主要介绍了React中setState/useState的使用方法,useState 和 setState 在React开发过程中 使用很频繁,但很多人都停留在简单的使用阶段,并没有正在了解它们的执行机制
    2023-04-04
  • react使用antd表单赋值,用于修改弹框的操作

    react使用antd表单赋值,用于修改弹框的操作

    这篇文章主要介绍了react使用antd表单赋值,用于修改弹框的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • react中关于Context/Provider/Consumer传参的使用

    react中关于Context/Provider/Consumer传参的使用

    这篇文章主要介绍了react中关于Context/Provider/Consumer传参的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • 在React中编写样式的六种方式

    在React中编写样式的六种方式

    在React中,编写样式主要有以下几种方式,内联样式,外部样式表,CSS Modules,Styled Components,Emotion和Radium这六种样式,下面我将针对上面提到的6种方式给出详细的代码示例,需要的朋友可以参考下
    2024-01-01
  • React Native 中添加自定义字体的方法

    React Native 中添加自定义字体的方法

    这篇文章主要介绍了如何在 React Native 中添加自定义字体,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08

最新评论