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

用户2090879

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

@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>

无论我多么努力,我都无法弄清楚如何让它发挥作用。

萨娜·蒙塔兹

制作<div>一个列表项 ie<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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章