我创建了一个选项卡,每个选项卡中都包含每个组件。选项卡大小看起来像一个矩形形状,但我想以这种格式获得一个像箭头形状的选项卡。在这里,我添加了带有组件的每个选项卡链接,但我无法做类似箭头的选项卡设计。下面给出了如何使用 css 代码片段制作类似选项卡的形状
import React, { useState } from 'react';
import './styles.css';
const TabContent = ({ children }) => {
return (
<div style={{ width: '100vw', height: '100vh', backgroundColor: 'gray' }}>
{children}
</div>
);
};
const Tab = ({ label, active, onClick }) => {
const tabClass = active ? 'tab active' : 'tab';
return (
<div className={tabClass} onClick={onClick}>
{label}
</div>
);
};
const App = () => {
const [activeTab, setActiveTab] = useState(1);
const handleTabClick = (tabIndex) => {
setActiveTab(tabIndex);
};
return (
<div className="tab-container">
<div style={{ display: 'flex' ,gap:'20px'}}>
<div style={{ flex: '0 0 180px', marginRight: '10px' }}>
<Tab label="Tab 1" active={activeTab === 1} onClick={() => handleTabClick(1)} />
<Tab label="Tab 2" active={activeTab === 2} onClick={() => handleTabClick(2)} />
<Tab label="Tab 3" active={activeTab === 3} onClick={() => handleTabClick(3)} />
</div>
<div style={{ flex: '1 1 auto' }}>
{activeTab === 1 && <TabContent>Component1</TabContent>}
{activeTab === 2 && <TabContent>Component2</TabContent>}
{activeTab === 3 && <TabContent>Component3</TabContent>}
</div>
</div>
</div>
);
};
export default App;
和CSS是
.tab-container {
display: flex;
flex-direction: column;
width: 200px;
height: 25vh;
}
.tab {
width: 100%;
padding: 10px;
text-align: center;
background-color: #f0f0f0;
border: 1px solid #434DB8;
color:#434DB8;
cursor: pointer;
border-top-right-radius:100%;
border-bottom-right-radius:100%;
}
.tab::after{
content: '';
position: absolute;
width: 1em;
height: 1em;
left: 30%;
border-bottom: 2px solid #434DB8;
border-right: 3px solid #434DB8;
border-left: 5px solid transparent;
transform: rotate(-45deg);
}
.tab.active {
background-color: #434DB8;
color:#FFFFFF;
font-weight: bold;
}
::after
这可以通过添加具有绝对位置的对角正方形的伪元素来完成。更多解释请参见代码注释: