Better Ken Burns
via http://codepen.io/peterwestendorp/pen/LbiwD
HTML
<div id="slideshow">
<img src="http://lorempixel.com/800/400/">
<img src="http://lorempixel.com/800/400/sports/">
<img src="http://lorempixel.com/800/400/city">
<img src="http://lorempixel.com/800/400/abstract">
<img src="http://lorempixel.com/800/400/food">
<img src="http://lorempixel.com/800/400/cats">
<img src="http://lorempixel.com/800/400/business">
<img src="http://lorempixel.com/800/400/animals">
</div>
CSS
/**
* See: http://www.css-101.org/articles/ken-burns_effect/css-transition.php
*/
/**
* Styling the container (the wrapper)
*
* position is used to make this box a containing block (it becomes a reference for its absolutely positioned children). overflow will hide part of the images moving outside of the box.
*/
#slideshow {
position:relative;
width:800px;
height:400px;
overflow:hidden;
border:8px solid #ccc;
}
/**
* Styling the images
*
* position:absolute is to put all images in a stack. Dimensions are set to increase the size of these images so their edges do not appear in the parent box when we move them inside the said box.
* Because the images are now larger than their parent container, we use top, left and margin values to align them in the center of the box.
* Finally, we set the transition (property and duration). Note that duration values are different for opacity and transform as we want the "fade-in" effect to be faster than the "panning" effect.
*/
#slideshow img {
position:absolute;
width:800px;
height:400px;
top:50%;
left:50%;
margin-left:-400px;
margin-top:-200px;
opacity:0;
-webkit-transition-property: opacity, -webkit-transform;
-webkit-transition-duration: 3s, 10s;
-moz-transition-property: opacity, -moz-transform;
-moz-transition-duration: 3s, 10s;
-ms-transition-property: opacity, -ms-transform;
-ms-transition-duration: 3s, 10s;
-o-transition-property: opacity, -o-transform;
-o-transition-duration: 3s, 10s;
transition-property: opacity, transform;
transition-duration: 3s, 10s;
}
/**
* We change the point of origin using four corners so images do not move in the same direction.
* This technique allows us to create various paths while applying the same translate() values to all images (see the 'fx' class further below).
*/
#slideshow img { transform-origin: bottom left; }
/*
#slideshow...
JavaScript
/**
* See: http://www.css-101.org/articles/ken-burns_effect/css-transition.php
*/
/**
* The idea is to cycle through the images to apply the "fx" class to them every n seconds.
* We can't simply set and remove that class though, because that would make the previous image move back into its original position while the new one fades in.
* We need to keep the class on two images at a time (the two that are involved with the transition).
*/
(function(){
// we set the 'fx' class on the first image when the page loads
document.getElementById('slideshow').getElementsByTagName('img')[0].className = "fx";
// this calls the kenBurns function every 4 seconds
// you can increase or decrease this value to get different effects
window.setInterval(kenBurns, 6000);
// the third variable is to keep track of where we are in the loop
// if it is set to 1 (instead of 0) it is because the first image is styled when the page loads
var images = document.getElementById('slideshow').getElementsByTagName('img'),
numberOfImages = images.length,
i = 1;
function kenBurns() {
if(i==numberOfImages){ i = 0;}
images[i].className = "fx";
// we can't remove the class from the previous element or we'd get a bouncing effect so we clean up the one before last
// (there must be a smarter way to do this though)
if(i===0){ images[numberOfImages-2].className = "";}
if(i===1){ images[numberOfImages-1].className = "";}
if(i>1){ images[i-2].className = "";}
i++;
}
})();