利用react做一个tab功能(组件)

2024年09月26日 建站教程

刚入门react的新手如何利用react做一个tab功能,下面web建站小编给大家介绍一下功能的实现方法。

1、tab.js 组件

import React from "react"
import { css } from "@emotion/core"
import { Link } from "gatsby"
import jdyStyles from "./container.css"

// TAB button 组件
export default props => {
  return (
  <li css={css`font-size: 18px;margin-left:18px;margin-right: 18px;display:flex;flex-direction: column;align-items:center;justify-content:center`} >
  <Link css={css`font-size: 18px;padding: 20px 12px;`} 
  className={ (props.selected?jdyStyles.header_hover_default:jdyStyles.header_hover)  }  to={props.to}>
  {props.children}
  </Link>
    
  </li>
  )
}

2、tab-layout.js 组件

import React from "react"
import { css } from "@emotion/core"
import { Link } from "gatsby"
import ListLink from "../components/tab"
import RegisterButton from "../components/round-button"
export default ({ tabIndex }) => {
  return (
    <div>
      {/* tab */}
      <ul style={{ listStyle: `none`, float: `right` }} css={css`display: flex;justify-content: space-between;align-items: center;`}>
        <ListLink to="/official-site/" selected={(tabIndex==='official-site')}>产品介绍</ListLink>
        <ListLink to="/about/" selected={(tabIndex==='about')}>成功案列</ListLink>
        <ListLink to="/contact/" selected={(tabIndex==='contact')}>服务支持</ListLink>
        <ListLink to="/sweet-pandas-eating-sweets/" selected={(tabIndex==='sweet-pandas-eating-sweets')}>资源中心</ListLink>
      </ul>
    </div>
  )
}

3、container.css

.header_hover{
  color: #333;
}

.header_hover_default{
  color: #0084ff!important;
  border-top: 3px solid #0084ff;
}

.header_hover:hover{
  color: #0084ff!important;
  border-top: 3px solid #0084ff;
}

本文链接:http://so.lmcjl.com/news/13962/

展开阅读全文
相关内容