import { Carousel } from "antd" import React, { useRef, useState } from "react" const CAROUSELDATA = ['wlm', 'zjsz', 'wysj', 'xxp', 'lss', 'yc', 'qh', 'tm'].map((item, index) => ({ image: `https://cp.71game.cn/assets/xjqxz/image/sect/sect${index + 1}.png`, icon: `https://cp.71game.cn/assets/xjqxz/image/sect/tab${index + 1}.png`, icon_selectd: `https://cp.71game.cn/assets/xjqxz/image/sect/tab${index + 1}_active.png`, value: item })) /** * 门派介绍 * @returns */ const SectBox: React.FC = () => { /****************************/ const ref = useRef() const [active, setActive] = useState(0) /****************************/ const scrollToTab = (e: React.MouseEvent) => { const tab = e.currentTarget; const tabContainer = tab.parentElement?.parentElement; // 获取 tab 的父容器 if (tabContainer) { const tabPosition = tab.getBoundingClientRect().left + tabContainer.scrollLeft; // 计算 tab 的绝对位置 const containerWidth = tabContainer.clientWidth; // 获取容器的宽度 const scrollPosition = tabPosition - containerWidth / 2 + tab.offsetWidth / 2; // 计算滚动位置 tabContainer?.scrollTo({ left: scrollPosition, behavior: 'smooth' // 平滑滚动 }); } }; return
{/*

门派介绍

*/}
{ setActive(nextSlide) }}> {CAROUSELDATA.map(item =>
)}
    {CAROUSELDATA?.map((item, index) =>
  • { scrollToTab(e) }} > { ref?.current?.goTo(index) }} />
  • )}
} export default React.memo(SectBox)