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

React實現(xiàn)菜單欄滾動功能

 更新時間:2024年03月25日 11:02:47   作者:卡卡舅舅  
本文將會基于react實現(xiàn)滾動菜單欄功能,點擊菜單,內(nèi)容區(qū)域會自動滾動到對應卡片,內(nèi)容區(qū)域滑動,指定菜單欄會被選中,代碼簡單易懂,感興趣的朋友一起看看吧

簡介

        本文將會基于react實現(xiàn)滾動菜單欄功能。

技術實現(xiàn)

實現(xiàn)效果

       點擊菜單,內(nèi)容區(qū)域會自動滾動到對應卡片。內(nèi)容區(qū)域滑動,指定菜單欄會被選中。

ScrollMenu.js

import {useRef, useState} from "react";
import './ScrollMenu.css';
export const ScrollMenu = ({products}) => {
    // 獲取 categoryProductMap
    const categoryProductMap = new Map();
    products.forEach(product => {
        const category = product.category;
        let categoryProductList = categoryProductMap.get(category);
        if (!categoryProductList) {
            categoryProductList = [];
        }
        categoryProductList.push(product);
        categoryProductMap.set(category, categoryProductList);
    });
    // 獲取類別列表
    const categoryList = Array.from(categoryProductMap.keys());
    // 菜單選中索引
    const [current, setCurrent] = useState(0);
    /**
     * 內(nèi)容引用
     */
    const contentRef = useRef();
    /**
     * 當左側菜單點擊時候
     */
    const onMenuClick = (idx) => {
        if (idx !== current) {
            // 內(nèi)容自動滾動到對應菜單位置
            contentRef.current.scrollTop = height.slice(0, idx).reduce((a, b) => a + b, 0);
            setCurrent(idx);
        }
    }
    /**
     *  計算右側商品類別卡片高度
     */
    const height = [];
    const itemHeight = 25;
    categoryList.forEach((category, index) => {
        var productCnt = categoryProductMap.get(category).length;
        height.push((productCnt + 1) * itemHeight); // 0.8 是header高度
    });
    console.log(height)
    /**
     * 當右側內(nèi)容滾動時候
     */
    const onContentScroll = () => {
        const scrollTop = contentRef.current.scrollTop;
        if (current < height.length - 1){
            const nextIdx = current + 1;
            // 計算下一個位置高度
            const nextHeight = height.slice(0, nextIdx).reduce((a, b) => a + b, 0);
            console.log('scrollTop', scrollTop, 'nextHeight', nextHeight, 'nextIdx', nextIdx)
            if (scrollTop >= nextHeight) {
                contentRef.current.scrollTop = nextHeight;
                setCurrent(nextIdx);
                return;
            }
        }
        if (current > 0) {
            const lastIdx = current - 1;
            // 計算上一個位置高度
            const lastHeight = height.slice(0, lastIdx).reduce((a, b) => a + b, 0);
            console.log('scrollTop', scrollTop, 'lastHeight', lastHeight, 'lastIdx', lastIdx)
            if (scrollTop <= lastHeight) {
                contentRef.current.scrollTop = lastHeight;
                setCurrent(lastIdx);
                return;
            }
        }
    }
    return (
        <div className='scroll-menu'>
            <div className='menu'>
                {
                    // 菜單列表
                    categoryList.map((category, index) => {
                        return (
                            <div className={"menu-item" + ((index === current )? '-active' : '')}
                                 key={`${index}`} id={`menu-item-${index}`}
                                 onClick={(event) => {
                                     onMenuClick(index)
                                 }}>
                                {category}
                            </div>
                        )
                    })
                }
            </div>
            <div className='content' ref={contentRef} onScroll={(event) => {
                onContentScroll()
            }}>
                {
                    categoryList.map((category, index) => {
                        // 獲取類別商品
                        const productList = categoryProductMap.get(category);
                        return (
                            <div key={index}>
                                <div className='content-item-header' key={`${index}`}
                                     id={`content-item-${index}`} style={{
                                    height: itemHeight
                                }} >{category}</div>
                                {
                                    productList.map((product,idx) => {
                                        return <div className='content-item-product'style={{
                                            height: itemHeight
                                        }}  key={`${index}-${idx}`} >{product.name}</div>
                                    })
                                }
                            </div>
                        )
                    })
                }
            </div>
        </div>
    )
}

ScrollMenu.css

.scroll-menu {
    display: flex;
    flex-direction: row;
    width: 300px;
    height: 100px;
}
.menu{
    width: 90px;
    height: 100px;
    display: flex;
    flex-direction: column;
}
.menu-item {
    text-align: center;
    vertical-align: middle;
}
.menu-item-active {
    text-align: center;
    vertical-align: middle;
    background-color: lightcoral;
}
.content {
    width: 210px;
    overflow: auto;
}
.content-item-header{
    text-align: left;
    vertical-align: top;
    background-color: lightblue;
}
.content-item-product{
    text-align: center;
    vertical-align: center;
    background-color: lightyellow;
}

App.js

import './App.css';
import {ScrollMenu} from "./component/scroll-menu/ScrollMenu";
const App = ()=> {
    const products = [
        {
            category:'蔬菜',
            name:'辣椒'
        },
        {
            category:'蔬菜',
            name:'毛豆'
        },
        {
            category:'蔬菜',
            name:'芹菜'
        },
        {
            category:'蔬菜',
            name:'青菜'
        },
        {
            category:'水果',
            name:'蘋果'
        },
        {
            category:'水果',
            name:'梨'
        },
        {
            category:'水果',
            name:'橘子'
        },   {
            category:'食物',
            name:'肉'
        },   {
            category:'食物',
            name:'罐頭'
        }
        ,   {
            category:'食物',
            name:'雞腿'
        }
    ];
    return (
        <ScrollMenu products={products}/>
    )
}
export default App;

到此這篇關于React實現(xiàn)菜單欄滾動的文章就介紹到這了,更多相關React 菜單欄滾動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • react實現(xiàn)原生下拉刷新

    react實現(xiàn)原生下拉刷新

    這篇文章主要為大家詳細介紹了react實現(xiàn)原生下拉刷新,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • react中JSX的注意點詳解

    react中JSX的注意點詳解

    這篇文章主要為大家詳細介紹了react中JSX的注意點,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • react反向代理使用http-proxy-middleware問題

    react反向代理使用http-proxy-middleware問題

    這篇文章主要介紹了react反向代理使用http-proxy-middleware問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 瀏覽器中視頻播放器實現(xiàn)的基本思路與代碼

    瀏覽器中視頻播放器實現(xiàn)的基本思路與代碼

    這篇文章主要給大家介紹了關于瀏覽器中視頻播放器實現(xiàn)的基本思路與代碼,并且詳細總結了瀏覽器中的音視頻知識,對大家的理解和學習非常有幫助,需要的朋友可以參考下
    2021-08-08
  • VSCode配置react開發(fā)環(huán)境的步驟

    VSCode配置react開發(fā)環(huán)境的步驟

    本篇文章主要介紹了VSCode配置react開發(fā)環(huán)境的步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • React Native使用百度Echarts顯示圖表的示例代碼

    React Native使用百度Echarts顯示圖表的示例代碼

    本篇文章主要介紹了React Native使用百度Echarts顯示圖表的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • 使用useMutation和React Query發(fā)布數(shù)據(jù)demo

    使用useMutation和React Query發(fā)布數(shù)據(jù)demo

    這篇文章主要為大家介紹了使用useMutation和React Query發(fā)布數(shù)據(jù)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • React 性能優(yōu)化之非必要的渲染問題解決

    React 性能優(yōu)化之非必要的渲染問題解決

    本文主要介紹了React 性能優(yōu)化之非必要的渲染問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • React 首頁加載慢問題性能優(yōu)化案例詳解

    React 首頁加載慢問題性能優(yōu)化案例詳解

    這篇文章主要介紹了React 首頁加載慢問題性能優(yōu)化案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • React中綁定this并給函數(shù)傳參的三種方式

    React中綁定this并給函數(shù)傳參的三種方式

    React中定義一個組件,可以通過ES6版本以前的React.createClass或者ES6的class xxx extends React.Component,綁定this是react中非常重要的一部分,React中,綁定this的方式有多種,下面一一看看怎么進行this綁定并給函數(shù)傳參,需要的朋友可以參考下
    2025-07-07

最新評論

平邑县| 许昌县| 余庆县| 闸北区| 荔浦县| 江川县| 青海省| 平山县| 正宁县| 简阳市| 双峰县| 儋州市| 莱西市| 漯河市| 泗阳县| 霍城县| 平塘县| 阜阳市| 太原市| 咸阳市| 丹江口市| 随州市| 嫩江县| 宽甸| 万宁市| 杭州市| 卓尼县| 民权县| 库车县| 洞口县| 都兰县| 南京市| 务川| 炎陵县| 九台市| 津市市| 拜城县| 宣化县| 黄山市| 高唐县| 谢通门县|