Can't flip the navbar menu text vertically using css animation

Viewed 131

I'm trying to create navbar menu in which the text flip when hover.

this is the code i tried so far and embedded the video which show the final result that i'm trying to achieve.

<!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">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            border: 0;
            vertical-align: baseline;
            background: transparent;
            font-weight: normal;
            text-decoration: none;
            outline: none;
            box-sizing: border-box;
        }
        .wrapper {
            width: 90%;
            margin: 0 auto;
            max-width:1100px;
        }
        header {
            height: 100px;
            padding: 20px 0;
        }
        header nav {}
        header nav ul {
            list-style: none;
            display: flex;
            justify-content: space-between;
        }
        header nav ul li {
            font-size: 23px;
            position: relative;
        }
        header nav ul li a {
            position: absolute;
        }
        header nav ul li a:hover {
            animation: flip 2s ease infinite;
        }
        div.video iframe {
            margin-top: 30px;
        }
        @keyframes flip {
            0% {
                top: 0;
            }
            25% {
                top: -50px;
            }
            50% {
                top: -100px;
            }
            75% {
                top: -150px;
            }
            100% {
                top: 0;
            }
        }
    </style>
</head>
<body>
    <header>
        <nav>
            <section class="wrapper">
                <ul>
                    <li><a href="#">About</a></li>
                    <li><a href="#">Project</a></li>
                    <li><a href="#">Resume</a></li>
                </ul>
            </section>
        </nav>
    </header>
    <div class="video">
        <section class="wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/WeUfsRce9_M" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
        <h3>Video link is given if iframe is note suported by stackoverflow code snippet poster</h3>
        <a href="https://www.youtube.com/embed/WeUfsRce9_M" target="_blank" >Video link</a>
        </section>
    </div>
</body>
</html>

I can't figure out a way to animate the text which go upward when we hover and return to the starting position from the bottom.

2 Answers

You need to use ::after and ::before selectors for this. Note that, you need to add data-text attributes to all the links (with the name of link as value).

* {
  margin: 0;
  padding: 0;
  border: 0;
  vertical-align: baseline;
  background: transparent;
  font-weight: normal;
  text-decoration: none;
  outline: none;
  box-sizing: border-box;
}

.wrapper {
  width: 90%;
  margin: 0 auto;
  max-width: 1100px;
}

header {
  height: 100px;
  padding: 20px 0;
}

header nav {}

header nav ul {
  list-style: none;
  display: flex;
  justify-content: space-between;
}

header nav ul li {
  font-size: 23px;
  position: relative;
}

header nav ul li a {
  position: absolute;
  height: 1.3em;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

header nav ul li a::after {
  content: attr(data-text);
  color: red;
  transition: transform 0.3s;
}

header nav ul li a::before {
  content: attr(data-text);
  color: blue;
  transition: transform 0.3s;
}

header nav ul li a:hover::before {
  transform: translateY(-1.15em);
}

header nav ul li a:hover::after {
  transform: translateY(-1.15em);
}

div.video iframe {
  margin-top: 30px;
}
<!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">
  <title>Document</title>
</head>

<body>
  <header>
    <nav>
      <section class="wrapper">
        <ul>
          <li>
            <a data-text="About" href=""></a>
          </li>
          <li>
            <a data-text="Project" href=""></a>
          </li>
          <li>
            <a data-text="Resume" href=""></a>
          </li>
        </ul>
      </section>
    </nav>
  </header>
  <div class="video">
    <section class="wrapper">
      <iframe width="560" height="315" src="https://www.youtube.com/embed/WeUfsRce9_M" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
      <h3>Video link is given if iframe is note suported by stackoverflow code snpite poster</h3>
      <a href="https://www.youtube.com/embed/WeUfsRce9_M" target="_blank">Vide link</a>
    </section>
  </div>
</body>

</html>

You need to set the duration of the animation operation.

You must use : animation-iteration-count

Or delete infinite :

animation: flip 2s ease;

Enjoy !

Related