导航栏上的 CSS 悬停滑动效果

时间:2021-07-20 12:23:18

标签: html css css-animations

我正在尝试使用以下代码在导航栏上实现悬停滑动效果:

@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');
body {
    font-family: 'Open Sans', sans-serif;
    background: #2c3e50;
}
nav{
    position: relative;
    width: 590px;
    height: 50px;
    background: #34495e;
    border-radius: 8px;
    font-size: 0;
    box-shadow: 0 2px 3px 0 rgba(0,0,0,.1);
}
nav a{
    font-size: 15px;
    text-transform: uppercase;
    color: white;
    text-decoration: none;
    line-height: 50px;
    position: relative;
    z-index: 1;
    display: inline-block;
    text-align: center;
}
nav .animation{
    position: absolute;
    height: 100%;
    /* height: 5px; */
    top: 0;
    /* bottom: 0; */
    z-index: 0;
    background: #1abc9c;
    border-radius: 8px;
    transition: all .5s ease 0s;
}
nav a:nth-child(1){
    width: 100px;
}
nav .start-home, a:nth-child(1):hover~.animation{
    width: 100px;
    left: 0;
}
nav a:nth-child(2){
    width: 110px;
}
nav a:nth-child(2):hover~.animation{
    width: 110px;
    left: 100px;
}
nav a:nth-child(3){
    width: 100px;
}
nav a:nth-child(3):hover~.animation{
    width: 100px;
    left: 210px;
}
nav a:nth-child(4){
    width: 160px;
}
nav a:nth-child(4):hover~.animation{
    width: 160px;
    left: 310px;
}
nav a:nth-child(5){
    width: 120px;
}
nav a:nth-child(5):hover~.animation{
    width: 120px;
    left: 470px;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Navbar Menu Hover Effect</title>
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
    <nav>
      <a href="#">Home</a>
      <a href="#">About</a>
      <a href="#">Blog</a>
      <a href="#">Portfolio</a>
      <a href="#">Contact</a>
      <div class="animation start-home"></div>
    </nav>
  </body>
</html>

问题是我在尝试将上面的代码调整为我的导航栏 HTML 结构时遇到了一些困难,它看起来像这样:

<nav>
  <ul>
    <li>  <a href="#">Home</a> </li>
    <li>  <a href="#">About</a> </li>
    <li>  <a href="#">Blog</a> </li>
    <li>  <a href="#">Portfolio</a> </li>
    <li>  <a href="#">Contact</a> </li>
  </ul>
  <div class="animation start-home"></div>
</nav>

无论我多么努力,我都不知道如何让它发挥作用。

2 个答案:

答案 0 :(得分:2)

如果不更改 HTML 结构,很遗憾,您无法做到这一点。让我这么说的是 CSS 行 nav a:nth-child(x):hover ~ .animation。使用您的 HTML 结构,您无法根据悬停的 .animation 设置 li 样式,它们不是兄弟。要做到这一点,a(或在您的情况下为 li)必须与 .animation

位于同一个 DOM“分支”

我对 Drupal 主题了解不多,但是如果您可以在菜单中创建一个“假”项,然后将其应用到 animation 类,这可能会起作用。 (这会改变你的 HTML 结构)

@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');

body {
  font-family: 'Open Sans', sans-serif;
  background: #2c3e50;
}

nav {
  position: relative;
  width: 590px;
  height: 50px;
  background: #34495e;
  border-radius: 8px;
  font-size: 0;
  box-shadow: 0 2px 3px 0 rgba(0, 0, 0, .1);
}

ul {
  display: contents;
}

nav li {
  position: relative;
  z-index: 1;
  display: inline-block;
  text-align: center;
}

nav a {
  font-size: 15px;
  text-transform: uppercase;
  line-height: 50px;
  color: white;
  text-decoration: none;
}

nav .animation {
  position: absolute;
  height: 100%;
  /* height: 5px; */
  top: 0;
  /* bottom: 0; */
  z-index: 0;
  background: #1abc9c;
  border-radius: 8px;
  transition: all .5s ease 0s;
}

nav li:nth-child(1) {
  width: 100px;
}

nav .start-home,
li:nth-child(1):hover~.animation {
  width: 100px;
  left: 0;
}

nav li:nth-child(2) {
  width: 110px;
}

nav li:nth-child(2):hover~.animation {
  width: 110px;
  left: 100px;
}

nav li:nth-child(3) {
  width: 100px;
}

nav li:nth-child(3):hover~.animation {
  width: 100px;
  left: 210px;
}

nav li:nth-child(4) {
  width: 160px;
}

nav li:nth-child(4):hover~.animation {
  width: 160px;
  left: 310px;
}

nav li:nth-child(5) {
  width: 120px;
}

nav li:nth-child(5):hover~.animation {
  width: 120px;
  left: 470px;
}
<nav>
  <ul>
    <li> <a href="#">Home</a> </li>
    <li> <a href="#">About</a> </li>
    <li> <a href="#">Blog</a> </li>
    <li> <a href="#">Portfolio</a> </li>
    <li> <a href="#">Contact</a> </li>
    <li class="animation start-home"></li>
  </ul>
</nav>

仅更改 <a> 替换为 <li> 并在 display: contents 上添加 <ul>

答案 1 :(得分:2)

<div> 设为列表项,即 <li> 并在悬停在列表项上时应用动画。

@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');
body {
  font-family: 'Open Sans', sans-serif;
  background: #2c3e50;
}

nav {
  position: relative;
  width: 590px;
  height: 50px;
  background: #34495e;
  border-radius: 8px;
  font-size: 0;
  box-shadow: 0 2px 3px 0 rgba(0, 0, 0, .1);
}

nav a {
  font-size: 15px;
  text-transform: uppercase;
  color: white;
  text-decoration: none;
  line-height: 50px;
  position: relative;
  z-index: 1;
  display: inline-block;
  text-align: center;
}

nav .animation {
  position: absolute;
  height: 100%;
  /* height: 5px; */
  top: 0;
  /* bottom: 0; */
  z-index: 0;
  background: #1abc9c;
  border-radius: 8px;
  transition: all .5s ease 0s;
}

ul {
  display: flex;
}

nav li:nth-child(1) {
  width: 100px;
}

nav .start-home,
li:nth-child(1):hover~.animation {
  width: 100px;
  left: 0;
}

nav li:nth-child(2) {
  width: 110px;
}

nav li:nth-child(2):hover~.animation {
  width: 110px;
  left: 100px;
}

nav li:nth-child(3) {
  width: 100px;
}

nav li:nth-child(3):hover~.animation {
  width: 100px;
  left: 210px;
}

nav li:nth-child(4) {
  width: 160px;
}

nav li:nth-child(4):hover~.animation {
  width: 160px;
  left: 310px;
}

nav li:nth-child(5) {
  width: 120px;
}

nav li:nth-child(5):hover~.animation {
  width: 120px;
  left: 470px;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Navbar Menu Hover Effect</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <nav>
    <ul class="nav">
      <li> <a href="#">Home</a> </li>
      <li> <a href="#">About</a> </li>
      <li> <a href="#">Blog</a> </li>
      <li> <a href="#">Portfolio</a> </li>
      <li> <a href="#">Contact</a> </li>
      <li class="animation start-home"></li>
    </ul>

  </nav>
</body>

</html>