简体   繁体   English

NavLink:CSS animation 上 hover 和活动

[英]NavLink: CSS animation on hover and active

I'm trying to style a nav-bar with some CSS to add some animations to the NavLinks.我正在尝试使用一些 CSS 设置nav-bar的样式,以向 NavLinks 添加一些动画。 So I'm trying to accomplish that on hover the text will be underlined from right to left and when a NavLink is active I want it to be underlined.所以我试图在 hover 上实现这一点,文本将从右到左加下划线,当 NavLink 处于活动状态时,我希望它加下划线。

With my solution I get almost the behaviour I want except that when a NavLink is active the hover animation and the active underline don't overlay each other.通过我的解决方案,我几乎得到了我想要的行为,除了当 NavLink 处于活动状态时,hover animation 和活动下划线不会相互重叠。 The two underlines form two separate underlines below the text.两条下划线在文本下方形成两条单独的下划线。

Picture of current NavLink with hover and active当前带有 hover 和活动的 NavLink 的图片

Here is the code:这是代码:

{navItems.map((item) => (
    <div className="example">
        <NavLink
            key={item.title.toLowerCase()}
            className="hover hover-1"
            activeClassName="active"
            to={item.link}
            exact={item.exact}
        >
            {item.title}
        </NavLink>
    </div>
))}

And here is the CSS:这是 CSS:

@import "./../global";

$animate: all 0.2s ease-in-out;
$darkBlue: darkBlue;

.example {
  display: flex;
  flex-flow: row nowrap;
  align-items: bottom;
  margin: 0 0 5px;
  .hover {
    text-align: center;
    margin: 0 auto;
    padding: 0;
    transition: $animate;
    position: relative;
    &:before,
    &:after {
      content: "";
      position: absolute;
      bottom: 0px;
      width: 0px;
      height: 4px;
      margin: 5px 0 0;
      transition: $animate;
      transition-duration: 0.5s;
      opacity: 0;
      background-color: $darkBlue;
    }

    &.hover-1 {
      &:before,
      &:after {
        left: 0;
      }
    }
  }
  &:hover {
    cursor: pointer;
    .hover {
      &:before,
      &:after {
        width: 100%;
        opacity: 1;
      }
    }
  }
  .active {
      border-bottom: 4px solid $darkBlue;
  }
}

I would very much appreciate some help with this issue:)我非常感谢您对这个问题的一些帮助:)

You can achieve your requirement by apply not in css like below.您可以通过不在css 中申请来满足您的要求,如下所示。

.example {
 // remaining properties 

 // EVERYTHING IS SAME BUT JUST ADDED not
.hover:not(.active) {

text-align: center;
margin: 0 auto;
padding: 0;
transition: $animate;
position: relative;
&:before,
&:after {
  content: "";
  position: absolute;
  bottom: 0px;
  width: 0px;
  height: 4px;
  margin: 5px 0 0;
  transition: $animate;
  transition-duration: 0.5s;
  opacity: 0;
  background-color: $darkBlue;
}

&.hover-1 {
  &:before,
  &:after {
    left: 0;
  }
 }
}
 // remaining properties
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM