react實現(xiàn)頭部導(dǎo)航,選中狀態(tài)底部出現(xiàn)藍色條塊問題
更新時間:2023年11月14日 08:57:33 作者:HaanLen
這篇文章主要介紹了react實現(xiàn)頭部導(dǎo)航,選中狀態(tài)底部出現(xiàn)藍色條塊問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
導(dǎo)航樣式,選中item底部藍色
const [itemIndex, setItemIndex] = useState(0);
<div className="box" onClick={(e) => { console.log('e', e.target?.dataset); if (!e.target?.dataset?.index) { return; }; setItemIndex(Number(e.target?.dataset?.index)); }} > <div className="top-item" style={{ left: `${itemIndex * 25}%` }}></div> <div className={`${itemIndex === 0 ? 'item-active' : ''} box-item item1`} data-index="0" >item1 </div> <div className={`${itemIndex === 1 ? 'item-active' : ''} box-item item2`} data-index="1" >item2 </div> <div className={`${itemIndex === 2 ? 'item-active' : ''} box-item item3`} data-index="2" >item3 </div> <div className={`${itemIndex === 3 ? 'item-active' : ''} box-item item4`} data-index="3" >item4 </div> </div>
利用border-bottom效果
.box { margin-top: 40px; width: 800px; display: flex; justify-content: space-around; height: 60px; font-size: 16px; align-items: center; //垂直居中 border-bottom: 1px solid #888; position: relative; .box-item { text-align: center; } .item-active { color: #1581ff; } .top-item { position: absolute; height: 3px; background-color: #1581ff; bottom: 0; width: calc(100% / 4); left: 0; } }
利用偽元素效果
.box { margin-top: 40px; width: 800px; display: flex; justify-content: space-around; height: 60px; font-size: 16px; align-items: center; //垂直居中 // border-bottom: 1px solid #888; position: relative; &::after { content: ""; width: 100%; height: 1px; position: absolute; left: 0; bottom: 0; // background-color: #e7e7e7; background-color: #888; } .box-item { text-align: center; } .item-active { color: #1581ff; } .top-item { position: absolute; height: 3px; background-color: #1581ff; bottom: 0; width: calc(100% / 4); left: 0; } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
React超詳細分析useState與useReducer源碼
我正在處理的組件是表單的時間輸入。表單相對復(fù)雜,并且是動態(tài)生成的,根據(jù)嵌套在其他數(shù)據(jù)中的數(shù)據(jù)顯示不同的字段。我正在用useReducer管理表單的狀態(tài),到目前為止效果很好2022-11-11React+Spring實現(xiàn)跨域問題的完美解決方法
這篇文章主要介紹了React+Spring實現(xiàn)跨域問題的完美解決方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08React動畫實現(xiàn)方案Framer Motion讓頁面自己動起來
這篇文章主要為大家介紹了React動畫實現(xiàn)方案Framer Motion讓頁面自己動起來,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10react 跳轉(zhuǎn)后路由變了頁面沒刷新的解決方案
最近在學(xué)習(xí)React的過程中遇到了路由跳轉(zhuǎn)后頁面不刷新的問題,本文就詳細的介紹一下解決方法,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06