悬停时每个字母的橡皮筋

时间:2018-12-28 16:52:52

标签: javascript html css

我如何在悬停时用力圈住每个字母?目前,我已经为每个字母上的颜色工作了,但似乎无法使橡皮筋效果发挥作用。我在做什么错了?

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);
  }
}

2 个答案:

答案 0 :(得分:2)

设置position: absolute;并使用手动间距,如下所示:

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

手动间距会有些不精确。它将在垂直方向上更好地工作。但是上面是效果的大致片段。

如果文本较长,则可以编写一个JQuery脚本来分隔文本。您可以按照this帖子中的说明找到字符的宽度。希望对您有帮助。

答案 1 :(得分:0)

另一个答案的修改版本,使用内联代码块(与某些浏览器不兼容,但允许较少的标记):

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