Often used as part of an image gallery or to show additional information, again this can be done in javascript by gradually changing the padding of elements. This often looks choppy on mobile devices, and frames can be missed if the animation is quick. CSS transitions plus transforms help out to make this a simple effect to create.
Have a look at a more complete example on the demos page.
For more information on these, check this updated post: cross browser implementation.
Image 1Image 2Image 3Image 4
Firstly, the mark up:
<div id="slide1_container"> <div id="slide1_images"> <img src="/images/Cirques.jpg" /> <img src="/images/Clown%20Fish.jpg" /> <img src="/images/Stones.jpg" /> <img src="/images/Summit.jpg" /> </div> </div> <p id="slide1_controls"> <span id="slide1-1">Image 1</span> <span id="slide1-2">Image 2</span> <span id="slide1-3">Image 3</span> <span id="slide1-4">Image 4</span> </p>
The CSS:
#slide1_controls span {
padding-right:2em;
cursor:pointer;
}
#slide1_container {
width:450px;
height:281px;
overflow:hidden;
position:relative;
}
#slide1_images {
position:absolute;
left:0px;
width:1800px;
-webkit-transition:all 1.0s ease-in-out;
-moz-transition:all 1.0s ease-in-out;
-o-transition:all 1.0s ease-in-out;
-ms-transition:all 1.0s ease-in-out;
transition:all 1.0s ease-in-out;
}
#slide1_images img {
padding:0;
margin:0;
float:left;
}
Again, I'm using javascript to bind events to the clickable controls. This time I'm adding the number of pixels to slide into the js, though I could have defined classes for this.
$(document).ready(function() {
$("#slide1-1").click(function() {
$("#slide1_images").css("left","0");
});
$("#slide1-2").click(function() {
$("#slide1_images").css("left","-450px");
});
$("#slide1-3").click(function() {
$("#slide1_images").css("left","-900px");
});
$("#slide1-4").click(function() {
$("#slide1_images").css("left","-1350px");
});
});
This code could be abstracted and improved, but we are looking to keep it simple here.
Note: Animating by transitioning transforms is hardware accelerated on iOS, making this a good option there.
Image 1Image 2Image 3Image 4
Exactly the same as Demo 1, but the JS looks like this: (times 5 for the vendor specific markup)
$(document).ready(function() {
$("#slide2-1").click(function() {
$("#slide2_images").css("-webkit-transform","translate(0px, 0px)");
});
$("#slide2-2").click(function() {
$("#slide2_images").css("-webkit-transform","translate(-450px, 0)");
});
$("#slide2-3").click(function() {
$("#slide2_images").css("-webkit-transform","translate(-900px, 0)");
});
$("#slide2-4").click(function() {
$("#slide2_images").css("-webkit-transform","translate(-1350px, 0)");
});
});
Please add any questions/corrections/extra info below. Please be courteous to other users.
blog comments powered by Disqus