In this tutorial, we are going to build an animated photo stack, which
will use all kinds of fancy effects to transition between a set of
images. The effects are implemented purely using CSS3, which means that
they run smoothly on modern browsers and mobile devices. We will also
make the photo stack advance automatically, so you can use it as a
slideshow.
The HTML
As always, the first step is to present the markup of the example. We
are starting with a regular HTML5 document in which we are including a
number of CSS/JS files:
index.html
04 | < meta charset = "utf-8" /> |
06 | < title >Animated CSS3 Photo Stack | Tutorialzine Demo</ title > |
09 | < link href = "assets/css/style.css" rel = "stylesheet" /> |
10 | < link href = "assets/css/animate.css" rel = "stylesheet" /> |
17 | style = "background-image:url(...)" >Landscape 5</ a ></ li > |
21 | < a href = "#" class = "arrow previous" ></ a > |
22 | < a href = "#" class = "arrow next" ></ a > |
26 | < script src = "assets/js/script.js" ></ script > |
The
#photos UL holds the photos that we will be
animating. For each photo, I’ve defined a LI item with an anchor element
inside it. The image is set as the background-image property of the
link. As you will see in the CSS part, I am using the
background-size property to
force the image to cover the entire width and height of the link. When
adding more photos, keep in mind that because they are positioned
absolutely, they will be shown in
reverse order (the last photo will be at the top).
In the head section of the document, I am including our main stylesheet and
animate.css
– the library that gives us those wonderful CSS3 animations. Before the
closing body tag, we have the jQuery library and script.js which we
will discuss next.
The JavaScript
To trigger the effects that the
animate library
gives us, we have to assign a class name to the element with the name
of the animation. We will also have to move the animated photo at the
bottom of the stack after the animation finishes, so that we can show
the next image. Here is what we need to do in order to make this example
work:
- First, we will listen for clicks on the arrows;
- Then, when a click occurs on the next arrow, we will trigger a
randomly chosen CSS animation by assigning a class name to the topmost
element of the stack (this is actually the last LI item because of the
positioning);
- After one second, when the animation completes, we will move the animated element before the other LIs with the prependTo jQuery method (this will push it to the bottom of the stack) and remove the classes that we’ve assigned above.
- For the previous arrow, we will do nearly the same, with the only
difference being that we will take the last image and place it at the
top of the stack before triggering the animation.
In addition, I will also add auto-advance functionality like we did in
this article. This will turn the example into a cool slideshow that stops the automated transitions when you click one of the arrows.
Here is what the code looks like:
assets/js/script.js
03 | var exits = [ 'fadeOut' , 'fadeOutDown' , 'fadeOutUpBig' , 'bounceOut' , 'bounceOutDown' , |
04 | 'hinge' , 'bounceOutUp' , 'bounceOutLeft' , 'rotateOut' , 'rotateOutUpLeft' , |
05 | 'lightSpeedOut' , 'rollOut' ]; |
07 | var entrances = [ 'fadeIn' , 'fadeInDown' , 'fadeInRight' , 'bounceIn' , 'bounceInRight' , |
08 | 'rotateIn' , 'rotateInDownLeft' , 'lightSpeedIn' , 'rollIn' , 'bounceInDown' ]; |
10 | var photos = $( '#photos' ), |
13 | $( '.arrow' ).click( function (e, simulated){ |
20 | e.stopImmediatePropagation(); |
32 | clearInterval(slideshow); |
37 | $( '.arrow.next' ).click( function (e){ |
42 | var elem = $( '#photos li:last' ); |
45 | elem.addClass( 'animated' ) |
46 | .addClass( exits[Math.floor(exits.length*Math.random())] ); |
48 | setTimeout( function (){ |
51 | elem.attr( 'class' , '' ).prependTo(photos); |
61 | $( '.arrow.previous' ).click( function (e){ |
66 | var elem = $( '#photos li:first' ); |
73 | .addClass( entrances[Math.floor(entrances.length*Math.random())] ); |
75 | setTimeout( function (){ |
78 | elem.attr( 'class' , '' ); |
88 | var slideshow = setInterval( function (){ |
91 | $( '.arrow.next' ).trigger( 'click' ,[ true ]); |
I haven’t used all the effects that animate.css provides, but you can find a full list at its
github page.
All that we are left to do, is to write a few CSS styles.
The CSS
I won’t be showing all of the styles here, only those that are directly responsible for the photo stack:
assets/css/styles.css
13 | background-color : #fff ; |
14 | box-shadow: 1px 1px 1px #ccc ; |
17 | -webkit-animation-duration: 1 s; |
18 | -moz-animation-duration: 1 s; |
19 | animation-duration: 1 s; |
28 | background- size : cover; |
To change the duration of the animations, you will have to supply the
animation-duration property. In the fragment above, I have set it to 1
second. More properties that you can set are animation-delay for the
delay before the animation is triggered, and animation-iteration-count
for the number of repetitions.
by
Martin Angelov
Source:
http://tutorialzine.com/2013/02/animated-css3-photo-stack/
0 komentar:
Post a Comment