Animation How to - Hover to animate background image position with CSS3 Background-position transition








Question

We would like to know how to hover to animate background image position with CSS3 Background-position transition.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.container {<!--from  www.j a va 2s.  c  o m-->
  background: url(http://www.java2s.com/style/download.png);
  width: 230px;
  height: 230px;
  background-position: left;
  -webkit-transition: background-position .1s ease-in;
  -moz-transition: background-position .1s ease-in;
  -o-transition: background-position .1s ease-in;
  transition: background-position .1s ease-in;
}

.container:hover {
  background-position: right;
}
</style>

</head>
<body>
  <div class="container"></div>
</body>
</html>

The code above is rendered as follows: