NOTE: A video walkthrough is available to accompany this post.
CSS Animations allow developers to animate the values of CSS properties using keyframes. These animations can be difficult to write because it is hard to visualize an animation just by writing out CSS in a text editor.
Let's use Blend to create an animation using transforms. The animation will have a "2.5D effect" - because it uses simple transforms on 2D objects to create a 3D-like effect.
- Open Blend for Visual Studio 2013 and create a new HTML (Windows Store) app, using the Blank App template:
- Let's add in an image to animate. From the Projects Panel, expand the images subfolder and drag the image "logo.scale-100.png" to the artboard.
- With the img element selected, go to the CSS Properties Panel and ensure that "inline style" is selected. Then expand the Animation category. (note: normally it is a best practice to create a class for the animation instead of using an inline style, but there are issues with this in the Blend 2013 RC Build - these issues will be fixed by RTM).
- In the Animation Pane, click the "+" button to add in a new animation. Enter the following values:
- Next, we can record the actual animation key frames. Click the "Edit Animation" button in the Animation Pane. At the bottom of the Blend workspace, you will see a timeline, and at the top of the artboard you will see a message "Animation recording is on."
Note that any changes you make to the CSS properties for the selected object will record a key frame in the animation. If at any time you want to stop keyframe recording, just click the red "record" button to the left of this message.
- In the Animation Pane, advance the yellow marker to the 1 second mark.
- In the CSS Properties Panel, expand the Transform Category and add a new Skew Transform using the dropdown selection:
- Set the Skew x to 90deg and the y to 45deg.
- Add a second transform, this time select a Translate Transform using the dropdown selection.
Set the Translate x property to 500px and the y property to 300px.
- In the Animation pane, click the "play" button to preview the animation. You can also "scrub" the animation by moving the timeline.
- To stop recording the animation, click the "Animation recording is on" button at the top of the artboard.
- Run the app by hitting F5, and witness the css animation!
Now that we have an animation, let's add some interactivity to it so that the user clicks the image to start the animation. We can do this without writing any code using Behaviors.
- First, let's change our animation so that it is in the Paused state. With the image selected, go to the CSS Properties Panel and expand the Animation category. Set the play-state property of the animation to "paused."
- Set the iteration-count property to "1" so that the animation only executes once.
- Go to the Assets Panel and select the Behaviors category. Then select the SetStyleAction.
- Drag the SetStyleAction onto the element on the artboard.
- In the CSS Properties Panel, expand the Behaviors Category and you will see a new EventTriggerBehavior and SetStyleAction have been added. Select the EventTriggerBehavior and Note that the default event is "click" - so this action will be executed when the user clicks it.
- Select the SetStyleAction and set the styleProperty to "animation-play-state" and the styleValue to "running"
- Run the app by pressing F5. Then click the img to execute the SetStyleAction and run the animation.