最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

React實現(xiàn)二級聯(lián)動效果(樓梯效果)

 更新時間:2021年09月10日 10:34:43   作者:燃燒的冰山..  
這篇文章主要為大家詳細介紹了React實現(xiàn)二級聯(lián)動效果,樓梯效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了React實現(xiàn)二級聯(lián)動效果的具體代碼,供大家參考,具體內(nèi)容如下

模仿餓了么實現(xiàn)一個二級聯(lián)動的效果;

import "../css/Leftrightlinkage.less";
import React, { Component } from "react";
 
export default class Leftrightlinkage 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" },
      ],
      LeftList: [
        { id: 1, title: "列表1", height: 600 },
        { id: 2, title: "列表2", height: 600 },
        { id: 3, title: "列表3", height: 600 },
        { id: 4, title: "列表4", height: 600 },
        { id: 5, title: "列表5", height: 600 },
        { id: 6, title: "列表6", height: 600 },
        { id: 7, title: "列表7", height: 600 },
        { id: 8, title: "列表8", height: 600 },
        { id: 9, title: "列表9", height: 600 },
        { id: 10, title: "列表10", height: 600 },
      ],
      curr: 0, //存儲下標
    };
 
    // 默認添加一個 因為第一個的scrollTop值是0
    this.LeftHeight = [0];
    // 滾動的開關(guān)
    this.Swich = true;
  }
 
  // 渲染完成獲取每一個列表距離頂部的距離
  componentDidMount() {
    // 定義為0 每次就可以循環(huán)加起來就是盒子距離頂部的距離
    this.Height = 0;
    // 循環(huán)獲取每一個的高
    for (var i = 0; i < this.state.LeftList.length - 1; i++) {
      this.Height += this.state.LeftList[i].height;
      this.LeftHeight.push(this.Height);
    }
  }
  //   點擊左側(cè)列表 點擊獲取下標
  FnTable(index) {
    // 點擊的時候讓右邊的滾動事件為false
    this.Swich = false;
    // 存儲下標
    this.setState({
      curr: index,
    });
    // 根據(jù)下標取出數(shù)組中對應(yīng)下標的scrollTop值  就讓右邊的scrollTop為數(shù)組中取出的值
    this.refs["leftItem"].scrollTop = this.LeftHeight[index];
  }
  FnScroll() {
    // 監(jiān)聽滾動
    this.scrollTop = this.refs["leftItem"].scrollTop;
 
    // 這邊用開關(guān)判斷是否執(zhí)行
    if (this.Swich) {
      // 存放下標
      let num = 0;
      // 循環(huán)取出數(shù)組中的數(shù)值
      for (var i = 0; i < this.LeftHeight.length - 1; i++) {
        if (this.scrollTop >= this.LeftHeight[i]) {
          num = i;
        }
      }
      // 存儲下標
      this.setState({
        curr: num,
      });
    }
    // 判斷滾動的值和數(shù)組中的值相等 開關(guān)為true
    if (this.scrollTop == this.LeftHeight[this.state.curr]) {
      setTimeout(() => {
        this.Swich = true;
      });
    }
  }
 
  render() {
    return (
      <div className="box">
        <div className="scroll">
          <div className="list-left">
            {this.state.list.map((item, index) => (
              <div
                className="left-item"
                ref="scrollLeft"
                className={this.state.curr === index ? "active" : "left-item"}
                key={item.id}
                onClick={this.FnTable.bind(this, index)}
              >
                {item.title}
              </div>
            ))}
          </div>
          <div
            className="list-right"
            ref="leftItem"
            onScroll={this.FnScroll.bind(this)}
          >
            {this.state.LeftList.map((item) => (
              <div
                className="right-item"
                key={item.id}
                style={{ height: item.height }}
              >
                <div className="item-title">{item.title}</div>
              </div>
            ))}
          </div>
        </div>
      </div>
    );
  }
}

CSS樣式,文件格式是Less格式

 .box {
     width: 100vw;
     height: 100vh;
 
     .scroll {
         width: 100vw;
         height: 100vh;
         display: flex;
 
         .list-left {
             width: 200px;
             height: 100vh;
             background: rgb(151, 151, 151);
 
             .left-item {
                 height: 120px;
                 text-align: center;
                 line-height: 120px;
                 color: #ffffff;
                 font-size: 36px;
                 border: 3px solid #ffffff;
                 box-sizing: border-box;
             }
 
             .active {
                 height: 120px;
                 text-align: center;
                 line-height: 120px;
                 color: #ffffff;
                 font-size: 36px;
                 border: 3px solid #ffffff;
                 background-color: #f100d9;
                 box-sizing: border-box;
             }
         }
 
         .list-right {
             width: 100vw;
             height: 100vh;
             background-color: #15ff00;
             overflow: scroll;
 
             .right-item {
                 height: 400px;
                 border: 5px solid #0040ff;
                 font-size: 40px;
                 color: #ffffff;
                 box-sizing: border-box;
             }
         }
     }
 
 }

效果圖:

 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • ahooks解決React閉包問題方法示例

    ahooks解決React閉包問題方法示例

    這篇文章主要為大家介紹了ahooks解決React閉包問題方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • 詳解關(guān)于React-Router4.0跳轉(zhuǎn)不置頂解決方案

    詳解關(guān)于React-Router4.0跳轉(zhuǎn)不置頂解決方案

    這篇文章主要介紹了詳解關(guān)于React-Router4.0跳轉(zhuǎn)不置頂解決案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • react路由基礎(chǔ)解讀(Router、Link和Route)

    react路由基礎(chǔ)解讀(Router、Link和Route)

    這篇文章主要介紹了react路由基礎(chǔ)解讀(Router、Link和Route),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 詳解React?hooks組件通信方法

    詳解React?hooks組件通信方法

    這篇文章主要介紹了React?hooks組件通信,在開發(fā)中組件通信是React中的一個重要的知識點,本文通過實例代碼給大家講解react hooks中常用的父子、跨組件通信的方法,需要的朋友可以參考下
    2022-07-07
  • React Hooks中模擬Vue生命周期函數(shù)的指南

    React Hooks中模擬Vue生命周期函數(shù)的指南

    React Hooks 提供了一種在函數(shù)組件中使用狀態(tài)和其他 React 特性的方式,而不需要編寫類組件,Vue 的生命周期函數(shù)和 React Hooks 之間有一定的對應(yīng)關(guān)系,本文給大家介紹了React Hooks中模擬Vue生命周期函數(shù)的指南,需要的朋友可以參考下
    2024-10-10
  • 前端面試題必會之前端react面試題

    前端面試題必會之前端react面試題

    在前端面試過程中經(jīng)常會問到一些面試題,今天小編抽空給大家講解前端面試題之必會react面試題,需要的朋友可以參考下
    2023-03-03
  • react使用CSS實現(xiàn)react動畫功能示例

    react使用CSS實現(xiàn)react動畫功能示例

    這篇文章主要介紹了react使用CSS實現(xiàn)react動畫功能,結(jié)合實例形式分析了react使用CSS實現(xiàn)react動畫功能具體步驟與實現(xiàn)方法,需要的朋友可以參考下
    2020-05-05
  • React入門教程之Hello World以及環(huán)境搭建詳解

    React入門教程之Hello World以及環(huán)境搭建詳解

    Facebook 為了開發(fā)一套更好更適合自己的JavaScript MVC 框架,所以產(chǎn)生了react。后來反響很好,所以于2013年5月開源。下面這篇文章主要給大家介紹了關(guān)于React入門教程之Hello World以及環(huán)境搭建的相關(guān)資料,需要的朋友可以參考借鑒。
    2017-07-07
  • react 頁面加載完成后自動執(zhí)行標簽的點擊事件的兩種操作方法

    react 頁面加載完成后自動執(zhí)行標簽的點擊事件的兩種操作方法

    這篇文章主要介紹了react 頁面加載完成后自動執(zhí)行標簽的點擊事件,本文給大家分享兩種操作方法結(jié)合示例代碼給大家講解的非常詳細,需要的朋友可以參考下
    2022-12-12
  • React源碼state計算流程和優(yōu)先級實例解析

    React源碼state計算流程和優(yōu)先級實例解析

    這篇文章主要為大家介紹了React源碼state計算流程和優(yōu)先級實例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11

最新評論

崇左市| 广水市| 许昌县| 察雅县| 阿鲁科尔沁旗| 高尔夫| 龙口市| 四平市| 信丰县| 名山县| 嵩明县| 祁东县| 英山县| 浦江县| 孝感市| 平阳县| 翁源县| 九江县| 西充县| 天门市| 文成县| 黔南| 阜康市| 吉木萨尔县| 望江县| 永定县| 庄河市| 大丰市| 磐石市| 古浪县| 丰顺县| 县级市| 上虞市| 锡林郭勒盟| 噶尔县| 彭州市| 育儿| 县级市| 阿拉善左旗| 贵州省| 襄汾县|