0

I have created animation that runs hover the link.

I have created animation using in and out key frames.

But I got issue. I think this happens because of knight-rider-out animation. But I need that for mouse leave.

The issue is when the page load it is running automatically. Code pen link

.container {
  padding: 50px;
  max-width: 500px;
}

.dashBottom {
  padding-left: 0px;
  position: relative;
  padding-bottom: 15px;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  -ms-transition: all 0.3s;
  -o-transition: all 0.3s;
  transition: all 0.3s;
  color: black;
  text-decoration: none;
}
.dashBottom:after {
  position: absolute;
  content: "";
  bottom: 0px;
  width: 30px;
  background-color: black;
  height: 4px;
  left: 0;
  right: auto;
  -webkit-animation: knight-rider-out 0.5s;
  animation: knight-rider-out 0.5s;
  animation-fill-mode: forwards;
}
.dashBottom:hover:after {
  -webkit-animation: knight-rider-in 0.5s;
  animation: knight-rider-in 0.5s;
  animation-fill-mode: forwards;
}

@keyframes knight-rider-in {
  from {
    left: 0px;
    width: 30px;
  }
  50% {
    left: 0px;
    width: 100%;
  }
  to {
    left: calc( 100% - 30px);
    width: 30px;
  }
}
@keyframes knight-rider-out {
  from {
    left: calc( 100% - 30px);
    width: 30px;
  }
  50% {
    left: 0px;
    width: 100%;
  }
  to {
    left: 0px;
    width: 30px;
  }
}
<div class="container">
    <a class="viewMore dashBottom" href="#">View More</a>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
Sameera Liyanage
  • 1,381
  • 1
  • 12
  • 26

1 Answers1

3

To avoid this you can consider transition. Here is another way to achieve the same effect with less of code:

.container {
  padding: 50px;
  max-width: 500px;
}

.dashBottom {
  padding-left: 0px;
  position: relative;
  padding-bottom: 15px;
  transition: all 0.3s;
  color: black;
  text-decoration: none;
  background-image:linear-gradient(#000,#000);
  background-size: 200% 4px;
  background-position: calc(200% + 30px) 100%;
  background-repeat:no-repeat;
  transition:0.5s all;
}

.dashBottom:hover {
  background-position:calc(-100% - 30px)  100%;
}
<div class="container">
    <a class="viewMore dashBottom" href="#">View More</a>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415