SectBox.tsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { Carousel } from "antd"
  2. import React, { useRef, useState } from "react"
  3. const CAROUSELDATA = ['wlm', 'zjsz', 'wysj', 'xxp', 'lss', 'yc', 'qh', 'tm'].map((item, index) => ({
  4. image: `https://cp.71game.cn/assets/xjqxz/image/sect/sect${index + 1}.png`,
  5. icon: `https://cp.71game.cn/assets/xjqxz/image/sect/tab${index + 1}.png`,
  6. icon_selectd: `https://cp.71game.cn/assets/xjqxz/image/sect/tab${index + 1}_active.png`,
  7. value: item
  8. }))
  9. /**
  10. * 门派介绍
  11. * @returns
  12. */
  13. const SectBox: React.FC = () => {
  14. /****************************/
  15. const ref = useRef<any>()
  16. const [active, setActive] = useState<number>(0)
  17. /****************************/
  18. const scrollToTab = (e: React.MouseEvent<HTMLLIElement, MouseEvent>) => {
  19. const tab = e.currentTarget;
  20. const tabContainer = tab.parentElement?.parentElement; // 获取 tab 的父容器
  21. if (tabContainer) {
  22. const tabPosition = tab.getBoundingClientRect().left + tabContainer.scrollLeft; // 计算 tab 的绝对位置
  23. const containerWidth = tabContainer.clientWidth; // 获取容器的宽度
  24. const scrollPosition = tabPosition - containerWidth / 2 + tab.offsetWidth / 2; // 计算滚动位置
  25. tabContainer?.scrollTo({
  26. left: scrollPosition,
  27. behavior: 'smooth' // 平滑滚动
  28. });
  29. }
  30. };
  31. return <section className="sect-box">
  32. {/* <h1 className="news-title"><span>门派介绍</span></h1> */}
  33. <div className="hero-inner sect-inner">
  34. <Carousel dots={false} ref={ref} beforeChange={(_: number, nextSlide: number) => { setActive(nextSlide) }}>
  35. {CAROUSELDATA.map(item => <div key={item.value}>
  36. <img src={item.image} alt="" className="sect-img" />
  37. </div>)}
  38. </Carousel>
  39. <div className="sect-nav">
  40. <div className="left" />
  41. <div className="nav">
  42. <ul>
  43. {CAROUSELDATA?.map((item, index) => <li
  44. className={active === index ? 'active' : ''}
  45. key={item.value}
  46. onClick={(e) => {
  47. scrollToTab(e)
  48. }}
  49. >
  50. <img
  51. src={active === index ? item.icon_selectd : item.icon}
  52. alt=""
  53. onClick={() => {
  54. ref?.current?.goTo(index)
  55. }}
  56. />
  57. </li>)}
  58. </ul>
  59. </div>
  60. <div className="right" />
  61. </div>
  62. </div>
  63. </section>
  64. }
  65. export default React.memo(SectBox)