react实现头部导航,选中状态底部出现蓝色条块问题

 更新时间:2023年11月14日 08:57:33   作者:HaanLen  
这篇文章主要介绍了react实现头部导航,选中状态底部出现蓝色条块问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

导航样式,选中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 Virtual DOM前端框架全面分析

    React Virtual DOM前端框架全面分析

    这篇文章主要为大家介绍了React Virtual DOM前端框架全面分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • Javascript之提高React性能的技巧

    Javascript之提高React性能的技巧

    一些刚开始学习 React,或者从其他框架转入 React 的开发者,一开始可能不会太关注性能。因为需要一些时间来发现新学习的框架的性能缺点。这篇文章主要介绍提高React性能的技巧,感兴趣的同学可以参考阅读
    2023-04-04
  • React超详细分析useState与useReducer源码

    React超详细分析useState与useReducer源码

    我正在处理的组件是表单的时间输入。表单相对复杂,并且是动态生成的,根据嵌套在其他数据中的数据显示不同的字段。我正在用useReducer管理表单的状态,到目前为止效果很好
    2022-11-11
  • React+Spring实现跨域问题的完美解决方法

    React+Spring实现跨域问题的完美解决方法

    这篇文章主要介绍了React+Spring实现跨域问题的完美解决方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-08-08
  • 探究react-native 源码的图片缓存问题

    探究react-native 源码的图片缓存问题

    本篇文章主要介绍了探究react-native 源码的图片缓存问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • React 路由懒加载的几种实现方案

    React 路由懒加载的几种实现方案

    这篇文章主要介绍了React 路由懒加载的几种实现方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • 基于React编写一个全局Toast的示例代码

    基于React编写一个全局Toast的示例代码

    前些日子在做项目的时候,需要封装一个Toast组件,我想起之前用过的库,只要在入口文件中引入就可以在全局中使用,还是很方便的,借这次机会也来实现一下,所以本文介绍了React中如何编写一个全局Toast,需要的朋友可以参考下
    2024-05-05
  • React动画实现方案Framer Motion让页面自己动起来

    React动画实现方案Framer Motion让页面自己动起来

    这篇文章主要为大家介绍了React动画实现方案Framer Motion让页面自己动起来,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • react swiper@6.x 工作中遇到的问题处理方案

    react swiper@6.x 工作中遇到的问题处理方案

    本文总结了reactswiper@6.x版本的使用方法和配置,包括安装步骤和配置自动轮播及移入停止的选项,感兴趣的朋友跟随小编一起看看吧
    2025-01-01
  • react 跳转后路由变了页面没刷新的解决方案

    react 跳转后路由变了页面没刷新的解决方案

    最近在学习React的过程中遇到了路由跳转后页面不刷新的问题,本文就详细的介绍一下解决方法,需要的朋友们下面随着小编来一起学习学习吧
    2021-06-06

最新评论