網站首頁 編程語言 正文
前言
特殊需求下,由于組件的Tab欄改動過于繁瑣,需要原生寫,下面是React實現Tab欄的demo。
實現
- 聲明一個currentId用于標識當前選中項
- 根據currentId動態控制樣式
- 根據currentId動態展示內容
.jsx
import React, { useState } from "react"
const Nav = () => {
const [currentId, setCurrentId] = useState(1)
const options = [
{ label: "選項一", id: 1 },
{ label: "選項二", id: 2 },
{ label: "選項三", id: 3 },
{ label: "選項四", id: 4 },
]
const changeId = (id) => {
setCurrentId(id)
}
return (
<div className="test_box">
{/* Tab欄 */}
<ul className="test_nav">
{options.map((item) => {
return (
<li
key={item.id}
onClick={() => changeId(item.id)}
className={item.id == currentId ? "active" : ""}
>
{item.label}
</li>
)
})}
</ul>
{/* 內容區域 */}
<div style={{ height: 200, backgroundColor: "pink" }}>
<div style={{ display: currentId == 1 ? "block" : "none" }}>內容一</div>
<div style={{ display: currentId == 2 ? "block" : "none" }}>內容二</div>
<div style={{ display: currentId == 3 ? "block" : "none" }}>內容三</div>
<div style={{ display: currentId == 4 ? "block" : "none" }}>內容四</div>
</div>
</div>
)
}
export default Nav
css
.test_box{
width: 500px;
border: 1px solid #333;
.test_nav{
display: flex;
justify-content: space-around;
list-style: none;
margin: 0;
li{
width: 150px;
height: 50px;
text-align: center;
line-height: 50px;
cursor: pointer;
}
.active{
background-color: skyblue;
position: relative;
&::after{
content:"";
display: inline-block;
position: absolute;
width: 60%;
bottom: 0;
left: 20%;
border-bottom: 2px solid red;
}
}
}
}
原文鏈接:https://blog.csdn.net/owo_ovo/article/details/132305369
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-08-15 linux環境下查看日志文件的一些操作
- 2022-06-02 Python學習之隨機模塊random詳解_python
- 2022-08-26 利用Python實現自動化監控文件夾完成服務部署_python
- 2023-06-04 pycharm中下載的包但是import還是無法使用/報紅的解決方法_python
- 2022-12-04 Android性能優化死鎖監控知識點詳解_Android
- 2024-04-05 mybatis(mybatis-plus)報invalid bound statement (not
- 2022-03-08 用C語言實現鏈式棧介紹_C 語言
- 2022-11-29 將VSCode添加至右鍵的菜單欄
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支