網(wǎng)站首頁 編程語言 正文
首先是js模塊
import React, { useEffect, useRef, useState } from 'react'
import { SideBar } from 'antd-mobile'
import './louceng.css'
import { useThrottleFn } from 'ahooks'
const items = [
{ key: '1', title: '第一項(xiàng)', text: <div>12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)</div> },
{ key: '2', title: '第二項(xiàng)', text: <div>12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)</div> },
{ key: '3', title: '第三項(xiàng)', text: <div>12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)</div> },
{ key: '4', title: '第四項(xiàng)', text: <div>12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)12313212313第一項(xiàng)</div> },
]
export default () => {
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 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} style={{width:"100px"}}>
{items.map(item => (
<div style={{height:"600px"}} key={item.key}>
<h2 id={`anchor-${item.key}`}>{item.title}</h2>
{item.text}
</div>
))}
</div>
</div>
)
}
css樣式
.container {
height: 100vh;
background-color: #ffffff;
display: flex;
justify-content: flex-start;
align-items: stretch;
/* [data-prefers-color='dark'] & {
background-color: unset;
} */
}
.side {
flex: none;
}
.main {
flex: auto;
padding: 0 24px 32px;
overflow-y: scroll;
}
h2 {
margin: 0;
padding: 12px 0;
}
實(shí)現(xiàn)效果
?
原文鏈接:https://blog.csdn.net/m0_64544033/article/details/131637702
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-06-18 C語言?詳解字符串基礎(chǔ)_C 語言
- 2022-05-17 qt 解決Error while building/deploying project Hmi (k
- 2022-04-11 一篇文章帶你入門python之推導(dǎo)式_python
- 2022-08-02 Docker安裝Redis配置遠(yuǎn)程連接及踩坑_docker
- 2022-10-17 C++STL教程之vector模板的使用_C 語言
- 2022-03-11 C++實(shí)操之內(nèi)聯(lián)成員函數(shù)介紹_C 語言
- 2022-01-19 el-table 定位 跳轉(zhuǎn) 至指定 行 位置,保證數(shù)據(jù)更新后位置不變
- 2022-12-04 如何對csv文件數(shù)據(jù)分組,并用pyecharts展示_python
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支