首頁 > web前端 > js教程 > 主體

怎麼使用react實作一個tab元件

藏色散人
發布: 2022-10-25 17:19:01
原創
2204 人瀏覽過

使用react實作一個tab元件的方法:1、透過「export default props => {...}」方式建立TAB button元件;2、透過「tab-group-layout.js」元件來傳“tabIndex”,並設定預設選取的tab效果;3、用react繼承“react.component”元件裡的onMouseOver和OnMouseOut方法即可。

怎麼使用react實作一個tab元件

本教學操作環境:windows7系統、react18.0.0版、Dell G3電腦。

怎麼使用react實作一個tab元件?

react寫入Tab元件

使用react寫入TAB欄元件和對應hover事件(背景:在用gatsby開發頁面時,遇到這樣的元件效果,順便記錄一下)

1、效果

##      預設選取的tab選取效果和滑鼠放上去的hover效果

      

      當滑鼠滑過右側的tab時,也會有和第一個一樣的選取效果!

2、tab-button.js 元件     

import React from "react"
import { css } from "@emotion/core"
import { Link } from "gatsby"
import jdyStyles from "./container.module.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>
 
)
}
登入後複製

3、tab-group-layout.js 元件  

import React from "react"
import { css } from "@emotion/core"
import { Link } from "gatsby"
import ListLink from "../components/tab-button"
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===&#39;official-site&#39;)}>产品介绍</ListLink>
<ListLink to="/about/" selected={(tabIndex===&#39;about&#39;)}>成功案列</ListLink>
<ListLink to="/contact/" selected={(tabIndex===&#39;contact&#39;)}>服务支持</ListLink>
<ListLink to="/sweet-pandas-eating-sweets/" selected={(tabIndex===&#39;sweet-pandas-eating-sweets&#39;)}>资源中心</ListLink>
</ul>
 
</div>
 
)
}
登入後複製

 

#使用這個元件傳過來tabIndex設定預設選取的tab效果;也可以自己處理展示的邏輯

4、對應的css樣式container.module.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;
}
登入後複製

5、目前元件的hover使用的是css樣式控制,也可以用react繼承react.component元件裡的onMouseOver和OnMouseOut方法來實作

推薦學習:《

react影片教學

##############################################

以上是怎麼使用react實作一個tab元件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!