react實現頭部導航,選中狀態(tài)底部出現藍色條塊問題
更新時間:2023年11月14日 08:57:33 作者:HaanLen
這篇文章主要介紹了react實現頭部導航,選中狀態(tài)底部出現藍色條塊問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
導航樣式,選中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; } }
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
React超詳細分析useState與useReducer源碼
我正在處理的組件是表單的時間輸入。表單相對復雜,并且是動態(tài)生成的,根據嵌套在其他數據中的數據顯示不同的字段。我正在用useReducer管理表單的狀態(tài),到目前為止效果很好2022-11-11React動畫實現方案Framer Motion讓頁面自己動起來
這篇文章主要為大家介紹了React動畫實現方案Framer Motion讓頁面自己動起來,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10