iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >react如何实现导航栏二级联动
  • 641
分享到

react如何实现导航栏二级联动

2023-06-29 10:06:11 641人浏览 八月长安
摘要

这篇文章给大家分享的是有关React如何实现导航栏二级联动的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。具体内容如下效果图js代码import { Component } 

这篇文章给大家分享的是有关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如何实现导航栏二级联动”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

--结束END--

本文标题: react如何实现导航栏二级联动

本文链接: https://www.lsjlt.com/news/324229.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++ 生态系统中流行库和框架的贡献指南
    作为 c++++ 开发人员,通过遵循以下步骤即可为流行库和框架做出贡献:选择一个项目并熟悉其代码库。在 issue 跟踪器中寻找适合初学者的问题。创建一个新分支,实现修复并添加测试。提交...
    99+
    2024-05-15
    框架 c++ 流行库 git
  • C++ 生态系统中流行库和框架的社区支持情况
    c++++生态系统中流行库和框架的社区支持情况:boost:活跃的社区提供广泛的文档、教程和讨论区,确保持续的维护和更新。qt:庞大的社区提供丰富的文档、示例和论坛,积极参与开发和维护。...
    99+
    2024-05-15
    生态系统 社区支持 c++ overflow 标准库
  • c++中if elseif使用规则
    c++ 中 if-else if 语句的使用规则为:语法:if (条件1) { // 执行代码块 1} else if (条件 2) { // 执行代码块 2}// ...else ...
    99+
    2024-05-15
    c++
  • c++中的继承怎么写
    继承是一种允许类从现有类派生并访问其成员的强大机制。在 c++ 中,继承类型包括:单继承:一个子类从一个基类继承。多继承:一个子类从多个基类继承。层次继承:多个子类从同一个基类继承。多层...
    99+
    2024-05-15
    c++
  • c++中如何使用类和对象掌握目标
    在 c++ 中创建类和对象:使用 class 关键字定义类,包含数据成员和方法。使用对象名称和类名称创建对象。访问权限包括:公有、受保护和私有。数据成员是类的变量,每个对象拥有自己的副本...
    99+
    2024-05-15
    c++
  • c++中优先级是什么意思
    c++ 中的优先级规则:优先级高的操作符先执行,相同优先级的从左到右执行,括号可改变执行顺序。操作符优先级表包含从最高到最低的优先级列表,其中赋值运算符具有最低优先级。通过了解优先级,可...
    99+
    2024-05-15
    c++
  • c++中a+是什么意思
    c++ 中的 a+ 运算符表示自增运算符,用于将变量递增 1 并将结果存储在同一变量中。语法为 a++,用法包括循环和计数器。它可与后置递增运算符 ++a 交换使用,后者在表达式求值后递...
    99+
    2024-05-15
    c++
  • c++中a.b什么意思
    c++kquote>“a.b”表示对象“a”的成员“b”,用于访问对象成员,可用“对象名.成员名”的语法。它还可以用于访问嵌套成员,如“对象名.嵌套成员名.成员名”的语法。 c++...
    99+
    2024-05-15
    c++
  • C++ 并发编程库的优缺点
    c++++ 提供了多种并发编程库,满足不同场景下的需求。线程库 (std::thread) 易于使用但开销大;异步库 (std::async) 可异步执行任务,但 api 复杂;协程库 ...
    99+
    2024-05-15
    c++ 并发编程
  • 如何在 Golang 中备份数据库?
    在 golang 中备份数据库对于保护数据至关重要。可以使用标准库中的 database/sql 包,或第三方包如 github.com/go-sql-driver/mysql。具体步骤...
    99+
    2024-05-15
    golang 数据库备份 mysql git 标准库
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作