{"id":772,"date":"2014-09-15T21:49:29","date_gmt":"2014-09-16T02:49:29","guid":{"rendered":"http:\/\/art.buffalo.edu\/coursenotes\/art320\/?page_id=772"},"modified":"2025-09-30T14:05:16","modified_gmt":"2025-09-30T18:05:16","slug":"exercise-5-css-animation-interactivity","status":"publish","type":"page","link":"https:\/\/ubwp.buffalo.edu\/art320\/exercises\/exercise-5-css-animation-interactivity\/","title":{"rendered":"Exercise 5: CSS Animation &amp; Interactivity"},"content":{"rendered":"\n<h2 class=\"wp-block-heading has-large-font-size\">Overview<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Animation<\/em> is the <strong>change<\/strong> in the <strong>shape<\/strong>, <strong>size<\/strong>, <strong>color<\/strong>, <strong>orientation<\/strong> or <strong>position<\/strong> of an element <strong>over time<\/strong>, in either a two- or three-dimensional direction.<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Interactivity<\/em> is when an animation or change in the state of information occurs as a <strong>result of a user action<\/strong>.<\/p>\n<\/blockquote>\n\n\n\n<p>Beyond entertainment, animation and interactivity can be used to provide visual feedback which reassures the user that their actions have achieved the desired consequence.<\/p>\n\n\n\n<p>Purposeful animation and interactivity can also be used to enhance the <strong>meaning<\/strong> of information being delivered.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\">Animation<\/h2>\n\n\n\n<p>Animation is an <strong>optical illusion<\/strong> whereby sequences of still images (frames) flashed before the eyes are <strong>perceived by the brain to be connected<\/strong>, creating a sense of <strong>fluid motion<\/strong>.<\/p>\n\n\n\n<p>Conventional (film-based) animation displayed stills at a rate of <strong>24 frames-per-second<\/strong> (fps). Modern digital video is commonly shot at <strong>24<\/strong> fps to simulate a cinematic feel, <strong>30<\/strong> fps, or <strong>60<\/strong> fps.<\/p>\n\n\n\n<p>Digital animation created in apps like Adobe Animate, After Effects, or ToonBoom can be set at many different rates. Simple animation might be adequately replayed as low as 5 fps, but 12 is considered a more common minimum. <strong>If the frame rate is too low for a given sequence, the illusion will be lost at the playback will appear stroboscopic or flickering.<\/strong><\/p>\n\n\n\n<p>Historically, senior animators would draw &#8220;<strong>key<\/strong>&#8221; frames of an animation, and junior animators would draw the &#8220;<strong>in-between<\/strong>&#8221; frames, or &#8220;<strong>tweens<\/strong>&#8220;. In the digital world, the computer draws the tweens.<\/p>\n\n\n\n<p>CSS shares this concept with animation apps: You set the keyframes, and the CSS works with the browser to create the tweens.<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\">Animation Can Add Life to Inanimate Objects<\/h3>\n\n\n\n<p>For more than a century, animators have been guided by Disney&#8217;s &#8220;12 Principles of Animation&#8221;. Animation that includes these visual qualities aides our suspension of disbelief so that we can empathize with otherwise inanimate objects and illustrated characters. See &#8220;<a href=\"https:\/\/www.creativebloq.com\/advice\/understand-the-12-principles-of-animation\">Understand Disney&#8217;s 12 Principles of Animation<\/a>&#8221; from <a href=\"https:\/\/www.creativebloq.com\/advice\/understand-the-12-principles-of-animation\">creativebloq.com<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\">CSS Animation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CSS Animation uses only HTML and CSS that you are already familiar with.<\/li>\n\n\n\n<li>Animation can be used to provide visual feedback on interactive elements including nav links and buttons.<\/li>\n\n\n\n<li>Any HTML element can be made to change over time (position, scale, rotation\u2026)<\/li>\n\n\n\n<li>Any motion (or sound) on a web page should be added with consideration to accessibility and user experience. See Nielsen\/Norman Group article on &#8220;<a href=\"https:\/\/www.nngroup.com\/articles\/animation-duration\/?utm_source=Alertbox&amp;utm_campaign=1315d1ce9f-UXanimations_SteppedUserTasks_Choice_20200210&amp;utm_medium=email&amp;utm_term=0_7f29a2b335-1315d1ce9f-24091205\">Executing UX Animations: Duration and Motion Characteristics<\/a>&#8220;<\/li>\n\n\n\n<li>Other options for adding motion to the web\n<ul class=\"wp-block-list\">\n<li>JavaScript<\/li>\n\n\n\n<li>SVG<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><a href=\"http:\/\/daneden.github.io\/animate.css\/\">Examples<\/a>&nbsp;of types of animation from&nbsp;<a href=\"http:\/\/daneden.me\/\">Daniel Eden<\/a>.<\/p>\n\n\n\n<p>(View and copy the <a href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/animate.css\/4.1.1\/animate.css\">CSS source code<\/a> to add these animations to items on your pages. Be sure to credit the original source.)<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\">Simple CSS animations include:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>a <strong>keyframes<\/strong> rule<\/li>\n\n\n\n<li>an <strong>animation name<\/strong> to refer to<\/li>\n\n\n\n<li>a <strong>class<\/strong> to which can be added HTML elements that are to be acted upon<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\">Keyframe Rules<\/h3>\n\n\n\n<p>Keyframe rules are a list of actions that describe what properties will change and when over time.&nbsp;Keyframes can be declared as points along a timeline&nbsp;either using the keywords <code>from<\/code> and <code>to<\/code>; or by using a percentage.<\/p>\n\n\n\n<p>In this example, <code>drive<\/code> is the name of the animation that will be created. The <code>from<\/code> point is 0, or the object&#8217;s current position. The property that will be changed&nbsp;is <code>transform<\/code> and the attribute&nbsp;is <code>translateX to<\/code> a value of -400px. In other words, the object will begin movement from it&#8217;s original location to a point 400px along the X axis to the left.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@keyframes drive {<br>   from {transform: translateX(0);}<br>   to {transform: translateX(-2000px);}<br>}<\/pre>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/ubwp.buffalo.edu\/art320\/wp-content\/uploads\/sites\/103\/2020\/10\/car.mov\"><\/video><\/figure>\n\n\n\n<p>Percentage measurements are useful when animating among more than two states.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@keyframes drive {<br>   0% {transform: translateX(0);}<br>   30%  {transform: translateX(-50px);}<br>   100% {transform: translateX(-2000px);}<br>}<\/pre>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/ubwp.buffalo.edu\/art320\/wp-content\/uploads\/sites\/103\/2020\/10\/car2.mov\"><\/video><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Assigning the Animation to an HTML Element<\/h3>\n\n\n\n<p>A class which calls&nbsp;a keyframe rule can be assigned to an HTML element. The class can contain a number of properties (see below.)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.car {\n    animation-name: drive;\n    animation-duration: 2s;\n    animation timing-function: ease-in;\n    animation-iteration-count: 1;\n    animation-fill-mode: forwards;\n    animation-delay: 3s;\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\">Animation Properties<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><strong>animation-name<\/strong><\/code><br>used to refer a CSS rule to a keyframe rule<\/li>\n\n\n\n<li><code><strong>animation-duration<\/strong><br><\/code>accepts values in seconds (s) and milliseconds (ms)<\/li>\n\n\n\n<li><code><strong>animation-timing-function<\/strong>: <\/code><code>ease-in<\/code> or <code>ease-out<\/code><br>Easing is the way speed is distributed across the duration of an animation<\/li>\n\n\n\n<li><code><strong>animation-iteration-count<\/strong><\/code><br>determines the number of times the animation will loop<\/li>\n\n\n\n<li><code><strong>animation-delay<\/strong><\/code><br>accepts values in seconds (s) and milliseconds (ms)<\/li>\n\n\n\n<li><code><strong>animation-fill-mode<\/strong><\/code>: <code>forwards<\/code>, <code>backwards<\/code>, or <code>both<\/code><br>Retains the objects style and returns it to either the first keyframe (backwards) last keyframe (forwards) or retains the styles at both keyframes (both.) Can be use to reset the objects position.<\/li>\n<\/ul>\n\n\n\n<p>For a complete list, see MDN&#8217;s List of <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/CSS_animated_properties\">Animated Properties<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\">Instructions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\" id=\"PrettyPhoto\">Part 1: Examine the PrettyPhoto Javascript\/JQuery Script<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Download the <a href=\"https:\/\/buffalo.box.com\/s\/dcv3rhbdyvlntnvfw6gl1ngp52uuj01i\">Ex05 Starter Files<\/a>. (On a Mac, if your browser does not automatically expand the .zip archive, double-click on it to extract the folder. On Windows, select the downloaded .zip archive and click the Extract button at the top of the File Explorer window.) Add your last name to the beginning of the folder name.<\/li>\n\n\n\n<li>Open gallery.html in your browser. Click on one of the bird image thumbnails. Notice how a full-sized version of the image zooms open, and the background becomes dimmed out. This &#8220;lightbox&#8221; effect is the result of the PrettyPhoto JavaScipt which has been added to the HTML.<\/li>\n\n\n\n<li>In order for the script to run, four things must be present. Note these instructions for use in your Project 1 Code:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The site folder contains the <strong>js<\/strong> folder (which contains <em>jquery.prettyPhoto.js<\/em>) and the <strong>css<\/strong> folder, which contains <em>prettyPhoto.css<\/em> and <em>resource.css<\/em>. <\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full has-custom-border is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"424\" height=\"622\" src=\"https:\/\/ubwp.buffalo.edu\/art320\/wp-content\/uploads\/sites\/103\/2022\/09\/image-17.png\" alt=\"\" class=\"wp-image-3237\" style=\"border-radius:0px\" srcset=\"https:\/\/ubwp.buffalo.edu\/art320\/wp-content\/uploads\/sites\/103\/2022\/09\/image-17.png 424w, https:\/\/ubwp.buffalo.edu\/art320\/wp-content\/uploads\/sites\/103\/2022\/09\/image-17-205x300.png 205w\" sizes=\"auto, (max-width: 424px) 100vw, 424px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In gallery.html, there is a snippet of code to go in the <code>&lt;head&gt;<\/code> section, which loads the jQuery library from Google servers, and the prettyPhoto script and css from the site folder:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;script src=\"http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.11.0\/jquery.min.js\"&gt;&lt;\/script&gt;\n&lt;script src=\"js\/jquery.prettyPhoto.js\"&gt;&lt;\/script&gt;\n&lt;link rel=\"stylesheet\" href=\"css\/prettyPhoto.css\" media=\"screen\"&gt;\n<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>and code to go before the closing <code>&lt;\/body&gt;<\/code> tag:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;script&gt;\n  $(document).ready(function(){\n   $(\"a[rel^='prettyPhoto']\").prettyPhoto({\n     social_tools:false,\n     animation_speed: 'fast', \/* fast\/slow\/normal *\/\n     slideshow: 5000, \/* false OR interval time in ms *\/\n     autoplay_slideshow: true, \/* true\/false *\/\n     opacity: 0.80, \/* Value between 0 and 1 *\/\n     show_title: true, \/* true\/false *\/\n   });\n });\n&lt;\/script&gt;<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Finally, the code for each image needs to be structured like the images in gallery.html. Looking at this example, we have an embedded image &lt;img&gt; wrapped in an anchor &lt;a&gt; tag, which makes the image clickable.<\/li>\n<\/ul>\n\n\n\n<p>The anchor tag&#8217;s <code>href<\/code> points to the full sized image, which loads when the image is clicked.<br>The anchor tag contains a <code>rel<\/code> attribute. This sets a relationship between the anchor and the page itself. In this case, it is a relationship to the prettyPhoto script. The image will also be contained within a group (or gallery) referred to as <code>pp_gal<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&lt;<span class=\"has-inline-color has-luminous-vivid-orange-color\">a<\/span> <span class=\"has-inline-color has-vivid-green-cyan-color\">href<\/span>=\"images\/full\/bird1.jpg\" <span class=\"has-inline-color has-vivid-green-cyan-color\">rel<\/span>=\"prettyPhoto[pp_gal]\" <span class=\"has-inline-color has-vivid-green-cyan-color\">title<\/span>=\"Robin\"&gt;&lt;<span class=\"has-inline-color has-luminous-vivid-orange-color\"><strong>img<\/strong> <\/span><span class=\"has-inline-color has-vivid-green-cyan-color\">src<\/span>=\"images\/thumbs\/bird1.png\" <span class=\"has-inline-color has-vivid-green-cyan-color\">alt<\/span>=\"Red and black Robin.\" \/&gt;&lt;<span class=\"has-inline-color has-luminous-vivid-orange-color\">\/a<\/span>&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\">Part 2: Animate&nbsp;the movement of the gallery thumbnails outwards along the x axis 100px<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>In the <code>head<\/code> section of the HTML, notice the following three lines of code, which loads the jquery library from Google servers, and the prettyPhoto script and css from the site folder:<\/li>\n\n\n\n<li>Notice the &lt;script&gt; code, which must be placed before the closing &lt;\/body&gt; tag. The javascript must fire after all of the elements that it needs to act upon have loaded. Some of these values, such as <code>animation_speed<\/code> and <code>opacity<\/code> can be changed based on your preference.<\/li>\n\n\n\n<li>Locate the section prepared for Animation rules in the style.css file, just before the media queries begin. (Location within the file is not important \u2014 we&#8217;ll just put them here for organization.)<\/li>\n\n\n\n<li>Copy the below CSS class and paste into style.css in the Animation section:<br><pre><code>.moveLeft {<br>   animation-name: moveLeft;<br>   animation-duration: 1s;<br>   animation-timing-function: ease-in;<br>   animation-iteration-count: 1;<br>   animation-fill-mode: backwards;}<\/code><\/pre><\/li>\n\n\n\n<li>Copy and paste this keyframe rule into your CSS:<br><pre><code>@keyframes moveLeft {<br>   from {transform: translateX(0);}<br>   to {transform: translateX(-100px);}<br>}<\/code><\/pre><\/li>\n\n\n\n<li>Create the appropriate code for <code>.moveRight<\/code><\/li>\n\n\n\n<li>Add the classes <code>.moveLeft<\/code> and <code>.moveRight<\/code> to the appropriate gallery thumbnail <code>img<\/code> tags.<\/li>\n\n\n\n<li>Save and test your gallery page.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\">Part 3: Animate each thumbnail to roll slightly inwards then outwards on hover<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li> Duplicate&nbsp;the CSS rules from part 1 by copying and pasting into new lines.<\/li>\n\n\n\n<li>Change the name of the keyframe rules and class names to moveLeftRotate and moveRightRotate.<\/li>\n\n\n\n<li>Add the :hover pseudo class to the CSS rule&nbsp; (e.g. .moveLeftRotate:hover)<\/li>\n\n\n\n<li>Also in the CSS rule, change the duration to 2s, and the timing-function to ease-in-out.<\/li>\n\n\n\n<li>Change the &lt;img&gt;&nbsp;class name to <code>moveLeftRotate<\/code> and <code>moveRightRotate<\/code><\/li>\n\n\n\n<li>Test your animation so far in Safari.<\/li>\n\n\n\n<li>Replace your <code>.moveLeftRotate<\/code> keyframe rule with the code below to add .5 rotation as the thumbnail moves 20px inward, out -250px at the 60% mark rotating 2x, then back to the starting point at the 100% mark, rotating in the opposite direction. Create a keyframes rule for <code>.moveRightRotate<\/code> making the necessary adjustments.<br><pre><code>@keyframes moveLeftRotate {<\/code><br>   <code>20% {transform: translateX(20px) rotate(0.5turn);}<\/code><br>   <code>60% {transform: translateX(-250px) rotate(-2turn);}<\/code><br>   <code>100% {transform: translateX(0px) rotate(2turn);}<\/code><br><code>}<\/code><\/pre><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\"> Part 4: Modify Transitions in Nav Link Hover<\/h3>\n\n\n\n<p>While the above keyframe based CSS animations provide great flexibility and creative opportunities, CSS3 Transitions can be used to quickly apply direct manipulations between two states of an object.<\/p>\n\n\n\n<p>The css rule for <code>nav li a:hover<\/code> has a <code>transition<\/code> property with a value of 1s. That means that these hover properties will transition in from the normal <em>non-hover<\/em> state to the <em>hover<\/em> state over the span of one second.<\/p>\n\n\n\n<p>Modify the values in <code>nav li a:hover<\/code> and observe how the changes appear.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>nav li a:hover {\n  color: #eea6a6;\n  text-decoration: underline;\n  background-color: #787878;\n  transition: 1s;\n}<\/code><\/pre>\n\n\n\n<p>For more details, see MDN&#8217;s &#8220;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/CSS_Transitions\/Using_CSS_transitions\">Using CSS transitions<\/a>&#8220;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-medium-font-size\">Submit<\/h3>\n\n\n\n<p>Upload your completed <a href=\"https:\/\/buffalo.box.com\/s\/3uhdtgf2o48esohsw9n7tti918302h4p\">Exercise 5 folder<\/a> to Box. Make sure the folder name begins with your last name.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Animation is the change in the shape, size, color, orientation or position of an element over time, in either a two- or three-dimensional direction. Interactivity is when an animation [&hellip;]<\/p>\n","protected":false},"author":295,"featured_media":0,"parent":35,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_EventAllDay":false,"_EventTimezone":"","_EventStartDate":"","_EventEndDate":"","_EventStartDateUTC":"","_EventEndDateUTC":"","_EventShowMap":false,"_EventShowMapLink":false,"_EventURL":"","_EventCost":"","_EventCostDescription":"","_EventCurrencySymbol":"","_EventCurrencyCode":"","_EventCurrencyPosition":"","_EventDateTimeSeparator":"","_EventTimeRangeSeparator":"","_EventOrganizerID":[],"_EventVenueID":[],"_OrganizerEmail":"","_OrganizerPhone":"","_OrganizerWebsite":"","_VenueAddress":"","_VenueCity":"","_VenueCountry":"","_VenueProvince":"","_VenueState":"","_VenueZip":"","_VenuePhone":"","_VenueURL":"","_VenueStateProvince":"","_VenueLat":"","_VenueLng":"","_VenueShowMap":false,"_VenueShowMapLink":false,"footnotes":""},"class_list":["post-772","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/ubwp.buffalo.edu\/art320\/wp-json\/wp\/v2\/pages\/772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubwp.buffalo.edu\/art320\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ubwp.buffalo.edu\/art320\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ubwp.buffalo.edu\/art320\/wp-json\/wp\/v2\/users\/295"}],"replies":[{"embeddable":true,"href":"https:\/\/ubwp.buffalo.edu\/art320\/wp-json\/wp\/v2\/comments?post=772"}],"version-history":[{"count":72,"href":"https:\/\/ubwp.buffalo.edu\/art320\/wp-json\/wp\/v2\/pages\/772\/revisions"}],"predecessor-version":[{"id":4194,"href":"https:\/\/ubwp.buffalo.edu\/art320\/wp-json\/wp\/v2\/pages\/772\/revisions\/4194"}],"up":[{"embeddable":true,"href":"https:\/\/ubwp.buffalo.edu\/art320\/wp-json\/wp\/v2\/pages\/35"}],"wp:attachment":[{"href":"https:\/\/ubwp.buffalo.edu\/art320\/wp-json\/wp\/v2\/media?parent=772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}