Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rubber band for each letter on hover

How can i rubber band each letter on hover? Currently i have it working for color on each letter but cant seem to get the rubber band effect going. What i'm doing wrong?

https://codepen.io/MariusZMM/pen/aPLJGo

.a {
  animation-duration: 1s;
  animation-fill-mode: both;
  animation-iteration-count: 1;
}

.a:hover {
  color: orange;
  /* animation: rubberBand 5s infinite; */
  animation-name: rubberBand;
}

@keyframes rubberBand {
  from {
    transform: scale3d(1, 1, 1);
  }

  30% {
    transform: scale3d(1.25, 0.75, 1);
  }

  40% {
    transform: scale3d(0.75, 1.25, 1);
  }

  50% {
    transform: scale3d(1.15, 0.85, 1);
  }

  65% {
    transform: scale3d(.95, 1.05, 1);
  }

  75% {
    transform: scale3d(1.05, .95, 1);
  }

  to {
    transform: scale3d(1, 1, 1);
  }
}
like image 269
Marius Avatar asked Oct 22 '25 17:10

Marius


2 Answers

Set position: absolute; and use manual spacing like so:

.a {
  animation-duration: 1s;
  animation-fill-mode: both;
  animation-iteration-count: 1;
  position: absolute;
}

.a:hover {
  color: orange;
  /* animation: rubberBand 5s infinite; */
  animation-name: rubberBand;
}

@keyframes rubberBand {
  from {
    transform: scale3d(1, 1, 1);
  }

  30% {
    transform: scale3d(1.25, 0.75, 1);
  }

  40% {
    transform: scale3d(0.75, 1.25, 1);
  }

  50% {
    transform: scale3d(1.15, 0.85, 1);
  }

  65% {
    transform: scale3d(.95, 1.05, 1);
  }

  75% {
    transform: scale3d(1.05, .95, 1);
  }

  to {
    transform: scale3d(1, 1, 1);
  }
} 
  <div class="title">
    <h1><div class="a" style="left: 4vw;">T</div>
    <h1><div class="a" style="left: 8vw;">E</div>
    <h1><div class="a" style="left: 12vw;">S</div>
    <h1><div class="a" style="left: 16vw;">T</div>
  </div>
  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

The manual spacing will be a little imprecise. It will work far better oriented vertically. but above is a rough snippet of what the effect would look like.

If you have a longer text, you can probably write a JQuery script to space out the text. You can find the width of a character as demonstrated in this SO post. I hope this helps you.

Modified version of another answer, using inline-block (which is not as compatible with some browsers, but allows less markup):

.a {
  animation-duration: 1s;
  animation-fill-mode: both;
  animation-iteration-count: 1;
  display: inline-block;
}

.a:hover {
  color: orange;
  /* animation: rubberBand 5s infinite; */
  animation-name: rubberBand;
}

@keyframes rubberBand {
  from {
    transform: scale3d(1, 1, 1);
  }

  30% {
    transform: scale3d(1.25, 0.75, 1);
  }

  40% {
    transform: scale3d(0.75, 1.25, 1);
  }

  50% {
    transform: scale3d(1.15, 0.85, 1);
  }

  65% {
    transform: scale3d(.95, 1.05, 1);
  }

  75% {
    transform: scale3d(1.05, .95, 1);
  }

  to {
    transform: scale3d(1, 1, 1);
  }
} 
  <div class="title">
    <h1>
    <span class="a" >T</span>
    <span class="a" >E</span>
    <span class="a" >S</span>
    <span class="a" >T</span>
  </h1>
  </div>
  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
like image 32
Garrett Motzner Avatar answered Oct 25 '25 07:10

Garrett Motzner