效果如图
添加Tabs的id,防止一页多个Tabs
const tabsDivId = useRef(Math.random());
<Tabs id={
tabsDivId.current}>
{
children}
</Tabs>
当 Tabs 的标签页总长度超过可现实区域长度,出现左右箭头,否则不显示
const [navHeight,setNavHeight] = useState(0);
useEffect(() => {
const tabListWidth = document.getElementById(tabsDivId.current).children[0].children[0].children[0].clientWidth;
const tabsNavWidth = document.getElementById(tabsDivId.current).children[0].clientWidth;
if(tabListWidth-tabsNavWidth > 0){
setShowArrow(true)
}
setNavHeight(document.getElementById(tabsDivId.current).children[0].clientHeight)
},[])
将箭头样式添加到 Tabs 组件上,并且绑定点击事件
使用 useEffect ,页面渲染完成之后添加左右箭头样式,这里左右箭头宽度各20px,重新计算出标签页总宽度
useEffect(() => {
if(showArrow && showTabsArrow ){
// tab父元素
const tabDiv = document.getElementById(tabsDivId.current).children[0];
// 标签
const tabNavDiv = tabDiv.children[0];
tabNavDiv.style.width = 'calc( 100% - 40px )'
// 内容
const tabContentDiv = tabDiv.children[1];
const leftArrow = document.createElement('button');
leftArrow.style = 'width:20px;height:' + navHeight + 'px' + ';text-align:center;line-height:' + (navHeight-10)+'px' + ';border:none;font-size:20px;padding:0;cursor: pointer;'
leftArrow.innerHTML = "<"