日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

react 利用antd-mobile實(shí)現(xiàn)樓層效果

作者:筱闖~ 更新時間: 2023-07-14 編程語言

首先是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

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新