【问题标题】:Css underline animation when hover AND when active悬停和活动时的CSS下划线动画
【发布时间】:2022-01-25 06:24:00
【问题描述】:

我正在尝试制作一个简单的导航菜单,其中包含带有动画下划线的按​​钮。我正在使用 reactjs 库

如果按钮元素处于活动状态,我不知道如何让下划线立即可见。

.btn {
  position: relative;
  text-transform: uppercase;
  color: whitesmoke;
  font-size: 1.4rem;
  cursor: pointer;
}

.btn:hover {
  color: whitesmoke;
  text-decoration: none;
}

.btn:before {
  content: "";
  position: absolute;
  width: 100%;
  height: 0.105rem;
  left: 0;
  bottom: 0;
  visibility: hidden;
  background-color: whitesmoke;
  transform: scaleX(0);
  transition: all 0.3s ease-in-out 0s;
}

.btn:hover:before {
  transform: scaleX(1);
  visibility: visible;
}

button.active .btn:before {
  visibility: visible;
  transform: scaleX(1);
}
<a href="/">
  <button id="home" className="btn active">
              Home
            </button>
</a>
</div>
<div className="col-4 col-sm-2">
  <a href="/blog">
    <button id="blog" className="btn">
              Blog
            </button>
  </a>

【问题讨论】:

  • 您的 HTML 无效。此外,className 应该是 class,除非您使用的是 react 之类的库
  • button:active,而不是 button.active,在这种情况下,button .btn 应该是 button.btn - 但我会完全删除 btn 类,它是多余的。
  • @Dai 仍然无法正常工作。

标签: html css reactjs


【解决方案1】:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="./style.css">
  <title>Document</title>
</head>
<body>
  <a href="/">
  <button id="home" class="btn active">
              Home
            </button>
</a>
</div>
<div className="col-4 col-sm-2">
  <a href="/blog">
    <button id="blog" class="btn">
              Blog
            </button>
  </a>
</body>
</html>
.btn {
    position: relative;
    text-transform: uppercase;
    color: whitesmoke;
    font-size: 1.4rem;
    cursor: pointer;
    border: none;
    outline: none;
    background-color: #332e2e77;
    display: inline-block;
    margin-bottom: 2rem;
}

a {
    display: inline-block;
}

.btn:hover {
    color: whitesmoke;
    text-decoration: none;
}

.btn::before {
    content: '';
    position: absolute;
    width: 0%;
    height: 0.105rem;
    left: 0;
    bottom: 0;
    visibility: hidden;
    background-color: rebeccapurple;
    transition: all 0.3s ease-in-out;
    -webkit-transition: all 0.3s ease-in-out;
    -moz-transition: all 0.3s ease-in-out;
    -ms-transition: all 0.3s ease-in-out;
    -o-transition: all 0.3s ease-in-out;
}

.btn:hover::before {
    width: 100%;
    visibility: visible;
}

.btn.active {
    position: relative;
}

.btn.active::after {
    content: '';
    position: absolute;
    width: 100%;
    height: 0.105rem;
    left: 0;
    bottom: 0;
    background-color: rebeccapurple;
    transition: all 0.3s ease-in-out;
    -webkit-transition: all 0.3s ease-in-out;
    -moz-transition: all 0.3s ease-in-out;
    -ms-transition: all 0.3s ease-in-out;
    -o-transition: all 0.3s ease-in-out;
}



现在尝试悬停。 className -> 类,如 cmets 中所述 我使用了宽度 0% -> 宽度 100%;和下划线正确动画。

.btn:hover::之前

-> 你这样选择伪选择器

【讨论】:

  • class/className="active" 时仍然没有下划线(使用 reactjs lib)
  • @AndresJulia 我已经编辑了我的代码。活动类现在有固定的彩色线
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-23
  • 2017-05-19
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 2014-12-13
相关资源
最近更新 更多