Animation How to - Bounce text with CSS Animation








Question

We would like to know how to bounce text with CSS Animation.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#wrapper {<!--from  w  ww .java 2s . co  m-->
  width: 250px;
  height: 40px;
  background-color: #EEE;
  color: red;
}

.nowPlayAnimate {
  position: absolute;
  white-space: nowrap;
  overflow: hidden;
  -moz-animation: slidein 1s infinite linear alternate;
  -webkit-animation: slidein 1s infinite linear alternate;
  -o-animation: slidein 1s infinite linear alternate;
  animation: slidein 1s infinite linear alternate;
}

@-moz-keyframes slidein {
   from { transform:translateX(0);}
   to {transform: translateX(calc(-100% + 250px)); /* -100% + parent width */}
}
@-webkit-keyframes slidein {
   from { -webkit-transform:translateX(0);}
   to {-webkit-transform: translateX(calc(-100% + 250px));}
}
@-o-keyframes slidein {
   from { -o-transform:translateX(0);}
   to {-o-transform: translateX(calc(-100% + 250px));}
}
@keyframes slidein {
   from { transform:translateX(0);}
   to {transform: translateX(calc(-100% + 250px));}
}
</style>
<script type='text/javascript'>
window.onload=function(){
    var testtext="Animation";
    document.getElementById("list0title").innerHTML=testtext;
}
</script>
</head>
<body>
  <div id="wrapper">
    <div id="list0title" class="nowPlayAnimate"></div>
  </div>
</body>
</html>

The code above is rendered as follows: