網站首頁 編程語言 正文
本文實例為大家分享了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 ( ? ? ? ? ? ?? ? ? ? ? ? ? ?? ? ? ? ) ? ? } } export default Scroll? ? ? ? ? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? {this.state.list.map((item, index) =>? ? ? ? ? ? ? ? ? ?{item.title})} ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? {this.state.LeftList.map((item) =>? ? ? ? ? ? ? ? ?)} ? ? ? ? ? ? ? ? ? ?{item.title}
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; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } }
原文鏈接:https://blog.csdn.net/weixin_57163112/article/details/120145601
相關推薦
- 2024-03-22 【IDEA】Spring boot項目設置網站圖標favicon不生效
- 2022-10-11 React -配置文件中需要使用組件中異步請求到的數據
- 2023-03-21 通俗易懂的C語言快速排序和歸并排序的時間復雜度分析_C 語言
- 2022-05-20 關于Mybatis-plus中PaginationInterceptor分頁攔截器過時問題解決辦法
- 2022-06-30 python神經網絡tensorflow利用訓練好的模型進行預測_python
- 2022-12-13 C++?Boost實現數字與字符串轉化詳解_C 語言
- 2022-07-16 構建npm配置包
- 2023-06-16 Visual?Studio?如何創建C/C++項目問題_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支