侧边导航如何联动页面内容

create-react-app  #创建react项目

npm i lorem-ipsum #控制内容显示大小

npm install --save antd-mobile #移动端组件

 HTML代码:

import React, { useEffect, useRef, useState } from 'react'
import { SideBar } from 'antd-mobile'
import styles from './index.less'
import { LoremIpsum } from 'lorem-ipsum'

const lorem = new LoremIpsum({
  sentencesPerParagraph: {
    max: 8,
    min: 4,
  },
  wordsPerSentence: {
    max: 16,
    min: 4,
  },
})

const items = [
  { key: '1', title: '第一项', text: lorem.generateParagraphs(8) },
  { key: '2', title: '第二项', text: lorem.generateParagraphs(8) },
  { key: '3', title: '第三项', text: lorem.generateParagraphs(8) },
  { key: '4', title: '第四项', text: lorem.generateParagraphs(8) },
]

export default () => {


  return (
    <div className={styles.container}>
      <div className={styles.side} style={{display:'flex'}}>
        <SideBar
        style={{position:'fixed'}}
          className={styles.bar}
          activeKey={activeKey}
          onChange={key => {
            window.document.getElementById(`anchor-${key}`)?.scrollIntoView()
          }}
        >
          {items.map(item => (
            <SideBar.Item key={item.key} title={item.title} />
          ))}
        </SideBar>
        <div className={styles.main} style={{width:'65%',marginLeft:'106px'}} ref={mainElementRef}>
          {items.map(item => (
            <div key={item.key} className={styles.main_content}>
              <h2 id={`anchor-${item.key}`}>{item.title}</h2>
              {item.text}
            </div>
          ))}
        </div>
      </div>
    </div>
  )
}

JS代码:

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

  const handleScroll = () => {
    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)
  }

  const mainElementRef = useRef(null)

  useEffect(() => {
    const mainElement = mainElementRef.current
    if (!mainElement) return
    document.addEventListener('scroll', handleScroll)
    return () => {
      document.removeEventListener('scroll', handleScroll)
    }
  }, [])

侧边导航与右侧页面内容效果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值