React 实现楼层效果

 实现滑动切换左侧导航栏,点击滑动至指定位置

import React, { useEffect, useRef, useState } from 'react'
import { SideBar } from 'antd-mobile'
import "./css/louce.css"
import { useThrottleFn } from 'ahooks'


const Louce = () => {
    const items = [
        { key: '1', title: '第一项', text: [{ _id: "1", name: "商品1" }, { _id: "11", name: "商品12" }, { _id: "2", name: "商品13" }, { _id: "12", name: "商品14" }, { _id: "13", name: "商品15" }, { _id: "14", name: "商品16" }] },
        { key: '2', title: '第二项', text: [{ _id: "1", name: "商品1" }, { _id: "11", name: "商品12" }, { _id: "2", name: "商品13" }, { _id: "12", name: "商品14" }, { _id: "13", name: "商品15" }, { _id: "14", name: "商品16" }] },
        { key: '3', title: '第三项', text: [{ _id: "1", name: "商品1" }, { _id: "11", name: "商品12" }, { _id: "2", name: "商品13" }, { _id: "12", name: "商品14" }, { _id: "13", name: "商品15" }, { _id: "14", name: "商品16" }] },
        { key: '4', title: '第四项', text: [{ _id: "1", name: "商品1" }, { _id: "11", name: "商品12" }, { _id: "2", name: "商品13" }, { _id: "12", name: "商品14" }, { _id: "13", name: "商品15" }, { _id: "14", name: "商品16" }] },
    ]

    const [activeKey, setActiveKey] = useState('1')

    const { run: handleScroll } = useThrottleFn(
        () => {
            let currentKey = items[0].key
            for (const item of items) {
                const element = document.getElementById(`anchor-${item.key}`)
                if (!element) continue
                const rect = element.getBoundingClientRect()
                if (rect.top <= 0) {
                    currentKey = item.key
                } else {
                    break
                }
            }
            setActiveKey(currentKey)
        },
        {
            leading: true,
            trailing: true,
            wait: 100,
        }
    )

    const mainElementRef = useRef()
    useEffect(() => {
        const mainElement = mainElementRef.current
        if (!mainElement) return
        mainElement.addEventListener('scroll', handleScroll)
        return () => {
            mainElement.removeEventListener('scroll', handleScroll)
        }
    }, [])
    return (
        <div>
            <div className="container">
                <div className="side">
                    <SideBar
                        activeKey={activeKey}
                        onChange={key => {
                            document.getElementById(`anchor-${key}`)?.scrollIntoView()
                        }}
                    >
                        {items.map(item => (
                            <SideBar.Item key={item.key} title={item.title} />
                        ))}
                    </SideBar>
                </div>
                <div className="main" ref={mainElementRef}>
                    {items.map(item => (
                        <div key={item.key} className='mitem'>
                            <h2 id={`anchor-${item.key}`}>{item.title}</h2>
                            <div className='items'>
                                {item.text.map(itm => {
                                    return (
                                        <div>
                                            {itm.name}
                                        </div>
                                    )
                                })}
                            </div>
                        </div>
                    ))}
                </div>
            </div>
        </div>
    );
}

export default Louce;

css样式

.container {
    height: 100vh;
    background-color: #ffffff;
    display: flex;
    justify-content: flex-start;
    align-items: stretch;
   
  }
  
  .side {
    flex: none;
  }
  
  .main {
    flex: auto;
    padding: 0 24px 32px;
    overflow-y: scroll;
    
  }

  h2 {
    margin: 0;
    padding: 12px 0;
  }

  .mitem{
    height: 100vh;
  }

  .items{
    display: flex;
    
    flex-wrap: wrap;
  }
  .items>div{
    width: 100px;
    line-height: 50px;
  }

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
关于Vue· 美籍华人尤雨溪所作,文档健全,生态完整,与脸谱的React各领前端半边天下;· 相对于React,Vue相对于初学者更容易上手;· 目前统治C端市场的跨平台框架uni-app也是基于Vue的基础技术栈进行开发;· Vue与React在框架设计上有极高的相似度,上手Vue以后,我们可以轻松过渡到React学习;课程特色· 零基础教学,由浅入深,轻松诙谐,物超所值;· 基于官方文档教学,在学习Vue的同时学习阅读文档的方式,授人以鱼更授人以渔;· 课程包含Vuex和Vue-Router最新版内容,即所谓【Vue全家桶开发】;· 完整包含Vue3全部内容;· 含京东商城完整项目从零到一开发过程;· 含最新版@vue/cli的开发和生产环境打包流程;· 含Vite两套脚手架的开发和生产环境打包流程;· 含项目部署上线全流程;· 含自定义组件库高阶课程,带你从零到一完成电梯组件开发与发布;· 附Vue高频面试题50余道,OFFER轻松斩获;课程收益· 掌握Vue2+Vue3的框架基础知识;· 掌握Vue全家桶项目开发+打包+上线全流程;· 掌握自定义组件库技巧;· 掌握Vue高频面试题回答技巧;课程关键词Vue2 Vue3 Vuex Vue-RouterVue项目 Vue全家桶@vue/cli Vite 项目打包 项目上线 自定义组件库 Vue面试题写在最后就连太阳光照到地球都需要8分钟,所以,你也需要时间,耐心一点,持续修炼。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值