Let's say that I need to make an animation of zooming out the image. This image is set by the user of my site and I don't know its resolution. So, there is a random image on the page and when a visitor clicks on the button it should zoom out the image. JavaScript/jQuery is used only in my example to let you know, what's my point, so CSS approach is what I am looking for. Under this text, you can see my code example. When you click on the "toggle" anchor, it should zoom image away.
$(function() {
$('.toggle').click(function(e) {
e.preventDefault();
$('#test').toggleClass('active');
});
});
#test {
position: relative;
width: 500px;
height: 150px;
overflow: hidden;
}
#test img {
position: absolute;
top: 50%;
left: 50%;
width: 100%;
height: auto;
transform: translate(-50%, -50%);
transition: all 5s;
}
#test.active img {
width: auto;
height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test">
<img src="http://www.blueverticalstudio.com/wp-content/uploads/2011/05/1303508174-torre-europa-0180-500x1000.jpg" />
</div>
<a href="#" class="toggle">Toggle</a>
EDIT:
Since this question received a lot of views, I updated my old jsFiddle which I pointed out in the accepted answer as a solution. It is not good and you should not use it at all. There is a 99% chance you don't need to use it like that.