Responsive image
by Annett
HTML
<div class="image-wrap" id="wrapper">
<img src="http://demo.solemone.de/overflow-image-with-vertical-centering-for-responsive-web-design/img/marienkaefer-by-solemone.jpg" alt="your image">
</div>
CSS
.image-wrap {
max-height: 450px;
overflow: hidden;
max-width: 800px;
-webkit-transition: max-width .5s ease-out; /* Saf3.2+, Chrome */
-moz-transition: max-width .5s ease-out; /* FF4+ */
-ms-transition: max-width .5s ease-out; /* IE10? */
-o-transition: max-width .5s ease-out; /* Opera 10.5+ */
transition: max-width .5s ease-out;
}
.image-wrap img {
width: 100%;
-webkit-transition: margin-top .5s ease-out; /* Saf3.2+, Chrome */
-moz-transition: margin-top .5s ease-out; /* FF4+ */
-ms-transition: margin-top .5s ease-out; /* IE10? */
-o-transition: margin-top .5s ease-out; /* Opera 10.5+ */
transition: margin-top .5s ease-out;
}
@media only screen and (min-width: 1160px) {
.image-wrap { max-width: 400px; }
}
JavaScript
$(document).ready(function() {
var imageHeight, wrapperHeight, overlap, container = $('.image-wrap');
function centerImage() {
imageHeight = container.find('img').height();
wrapperHeight = container.height();
overlap = (wrapperHeight - imageHeight) / 2;
container.find('img').css('margin-top', overlap);
}
$(window).on("load resize", centerImage);
var el = document.getElementById('wrapper');
if (el.addEventListener) {
el.addEventListener("webkitTransitionEnd", centerImage, false); // Webkit event
el.addEventListener("transitionend", centerImage, false); // FF event
el.addEventListener("oTransitionEnd", centerImage, false); // Opera event
}
});