首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在选项卡下添加样式线并突出显示所选选项卡

在选项卡下添加样式线并突出显示所选选项卡
EN

Stack Overflow用户
提问于 2021-06-30 15:16:49
回答 1查看 88关注 0票数 1

我想在一组选项卡下面添加一条线,并突出显示选定的选项卡。我使用的是react和styled组件,当用户选择活动列表项时,列表项下面应该会有一个条突出显示。

这就是它目前的样子

这就是它应该是什么样子

这是我到目前为止所拥有的代码

代码语言:javascript
复制
import { List, ListItems, Div } from './messageTab.elements';
function MessageTab() {
    return (

        <Tabs>
            <div label="Recent">
                Recent component
            </div>
            <div label="Unread">
                Unread component
            </div>
            <div label="Groups">
                Groups component
            </div>
        </Tabs>

    )
}


function Tabs(props) {
    const [activeTab, setActiveTab] = useState(props.children[0].props.label)
    const switchTabs = (tab) => {
        setActiveTab(tab);
    }

    console.log(activeTab);
    return (
        <Div>
            <List>
                {props.children.map((child) => {
                    const { label } = child.props;
                    return (
                        <Tab
                            activeTab={activeTab}
                            key={label}
                            label={label}
                            onClick={switchTabs}
                        />
                    );
                })}
            </List>
        </Div>
    );
}

function Tab(props) {
    const { label, onClick, activeTab } = props;
    console.log(activeTab);
    console.log(label);
    return (
        <ListItems onClick={() => onClick(label)}>{label}</ListItems>
    );
}

以下是我的样式化组件

代码语言:javascript
复制
import styled from 'styled-components';

const ListItems = styled.li`
    margin-right : 6%;
    color: #9095A4;
    
`;

const List = styled.ul`
    display : flex;
    list-style-type : none;
    float : right;
    flex-direction: row;
    
`;

const Div = styled.div`
    display : flex;
    margin-left : 6%;
`;

export { List, ListItems, Div };
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-30 15:54:04

您可以使用::after伪选择器来创建和设置“选项卡”的样式。

代码语言:javascript
复制
const ListItems = styled.li`
  margin-right: 6%;
  color: #9095a4;

  ${(props) =>
    props.activeTab &&
    css`
      color: #0095ff;

      ::after {
        content: "";
        position: relative;
        height: 5px;
        margin-top: 0.5rem;
        border-radius: 5px 5px 0 0;
        background-color: #0095ff;
        display: block;
      }
    `}
`;

在列表中添加底部边框并设置宽度。

代码语言:javascript
复制
const List = styled.ul`
  display: flex;
  list-style-type: none;
  float: right;
  flex-direction: row;
  border-bottom: 1px solid lightgray;
  width: 100%;
`;

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68190075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档