{"id":7227,"date":"2018-07-24T19:27:17","date_gmt":"2018-07-24T19:27:17","guid":{"rendered":"https:\/\/www.crimsondesigns.com\/blog\/?p=7227"},"modified":"2019-07-16T19:36:59","modified_gmt":"2019-07-16T19:36:59","slug":"subtle-animations-css-javascript-part-2","status":"publish","type":"post","link":"https:\/\/www.crimsondesigns.com\/blog\/subtle-animations-css-javascript-part-2\/","title":{"rendered":"Subtle Animations with CSS &#038; Javascript: Part 2"},"content":{"rendered":"<h2>Beginning CSS3 animation<\/h2>\n<p>CSS3 gives us two primary ways of animating HTML elements:<\/p>\n<ol>\n<li><strong>&#8220;transition&#8221;\u00a0<\/strong>and<strong>\u00a0&#8220;transform&#8221;<\/strong> manipulate elements from one state to another<\/li>\n<li><strong>&#8220;CSS animations&#8221;<\/strong>\u00a0can control a complex series of movements during an animation<\/li>\n<\/ol>\n<hr \/>\n<p>1)<strong> CSS <code>Transitions<\/code><\/strong>\u00a0are used for creating a smooth transition from one state to another. Transitions specify the duration of an element. Without a transition, the change would be abrupt and take effect immediately. By using transitions with transforms, you can produce a smooth, gradual animation. We most often see transitions used on <a href=\"https:\/\/www.w3schools.com\/cssref\/sel_hover.asp\" target=\"_blank\" rel=\"noopener\"><strong>HOVER<\/strong><\/a> states.\u00a0Transitions can be applied a wide variety of CSS properties such as <code>opacity<\/code>, <code>border<\/code>, <code>height<\/code>, <code>width<\/code> and\u00a0<code>font-size.<\/code><\/p>\n<p><strong>We have 5 properties that allow us to control CSS <code>transition<\/code> animations:<\/strong><\/p>\n<ul>\n<li>transition-property<\/li>\n<li>transition-duration<\/li>\n<li>transition-timing-function<\/li>\n<li>transition-delay<\/li>\n<li>transition<\/li>\n<\/ul>\n<p class=\"special\">(Remember: properties are a part of a CSS rule.)<\/p>\n<p class=\"special\">Example A:<\/p>\n<div class=\"highlight\"><strong>selector <\/strong><code>{transition-property: <strong>property<\/strong>; transition-duration: <strong>duration<\/strong>; transition-timing-function: <strong>timing-function<\/strong>; transition-delay: <strong>delay<\/strong>}<\/code><\/div>\n<p>The transition property is the SHORTHAND version of the above four properties in example A.<\/p>\n<div class=\"highlight\"><strong>selector<\/strong>\u00a0<code>{transition: <strong>property duration timing-function delay<\/strong>;}<\/code><\/div>\n<p>There are two properties that are required in order for the transition to take effect:<\/p>\n<p><strong>1. <code>transition-property<\/code><\/strong><\/p>\n<p><strong>2. <code>transition-duration<\/code><\/strong><\/p>\n<p>Transition Example:<\/p>\n<div class=\"highlight\">\n<p><strong>.square<\/strong> {transition: border-color 0.5s ease-in 3s;}<\/p>\n<p><strong>.square:hover<\/strong> {border-color: #FF1654;}<\/p>\n<\/div>\n<p>You don&#8217;t have to know all the details. Just recognizing the shorthand code will be enough for you to be able to manipulate a CSS3 rule that you are copying and pasting into your own style sheet.<\/p>\n<hr \/>\n<p>2) The <strong><code>transform<\/code><\/strong> property allows you to skew, rotate, translate or scale an element.\u00a0Transforms are triggered when an element changes states, such as on mouse-hover or mouse-click.<\/p>\n<p><strong>CSS <code>transform<\/code> has 6 main properties:<\/strong><\/p>\n<ul>\n<li>scale<\/li>\n<li>translate<\/li>\n<li>rotate<\/li>\n<li>skew<\/li>\n<li>perspective<\/li>\n<li>matrix<\/li>\n<\/ul>\n<ul>\n<li><code>scale()<\/code>: affects the size of an element.<\/li>\n<li><code>translate()<\/code>: moves an element sideways or up and down.<\/li>\n<li><code>rotate()<\/code>: rotates an element clockwise.<\/li>\n<\/ul>\n<p>The <code>transform<\/code> SHORTHAND allows you to string the various transform methods into one property.<\/p>\n<p>Example of Transition and Transform on an Element:<\/p>\n<div class=\"highlight\">\n<pre>.scaleThis {\r\n  transform: scale(1, 1);\r\n  transition: transform 0.5s ease;\r\n}\r\n.box:hover .scaleThis {\r\n  transform: scale(1.5, 1.5);\r\n}\r\n\r\n<\/pre>\n<\/div>\n<hr \/>\n<p>3)<strong> CSS <code>Animations<\/code><\/strong> &#8211; CSS animations can control a COMPLEX series of movements. They are animations you create with the <strong>@keyframes<\/strong> and <strong>animation properties.<\/strong><\/p>\n<p>So there are basically two parts to these animations:<\/p>\n<ol>\n<li><strong>Keyframes<\/strong> &#8211; define the stages and styles of the animation.<\/li>\n<li><strong>Animation Properties<\/strong> &#8211; define <em>how<\/em> it is animated.<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5894 size-full\" src=\"https:\/\/www.crimsondesigns.com\/blog\/wp-content\/uploads\/2018\/07\/css3-keyframes.png\" alt=\"CSS3 @keyframes\" width=\"300\" height=\"65\" \/><\/p>\n<p><code class=\"inline\">@keyframes<\/code>\u00a0is the CSS\u00a0rule where the animation is created.<\/p>\n<p>The <strong><code>@keyframes<\/code><\/strong> CSS at-rule controls the INTERMEDIATE STEPS in a CSS animation sequence. You can specify when the change will happen in <strong>percentages<\/strong>, or with the keywords <strong>&#8220;from&#8221;<\/strong> and <strong>&#8220;to&#8221;<\/strong> (which is the same as <strong>0%<\/strong> and <strong>100%<\/strong>).<\/p>\n<p>0% is the beginning. 100% is the end. You can also add in between percentages, such as 60%.<\/p>\n<p>We need to define our\u00a0<code>@keyframes<\/code> rule somewhere in our CSS style sheet.\u00a0Let&#8217;s give the set a name of \u2018<strong>bounceIn<\/strong>\u2019. You can use any descriptive name that helps you remember it.<\/p>\n<p><strong>@keyframes Example with Transform:<\/strong><\/p>\n<div class=\"highlight\">\n<pre>@keyframes bounceIn {\r\n  0% {\r\n    <strong>transform:<\/strong> scale(0.1);\r\n    opacity: 0;\r\n  }\r\n  60% {\r\n    <strong>transform:<\/strong> scale(1.2);\r\n    opacity: 1;\r\n  }\r\n  100% {\r\n    <strong>transform:<\/strong> scale(1);\r\n  }\r\n}<\/pre>\n<\/div>\n<p class=\"special\">Notice all the curly braces in the example. They are nested in pairs { }.<\/p>\n<p>To make the animation work, you need to bind the\u00a0<code class=\"inline\">@keyframes<\/code> to a selector. @keyframes alone won&#8217;t do anything.<\/p>\n<p><strong>Animation Properties<\/strong><\/p>\n<p>The <code class=\"inline\">animation<\/code> property is used to call <code class=\"inline\">@keyframes<\/code> inside a CSS selector.\u00a0The animation (applied to an element) can be controlled with different animation properties. You can decide such things as how many times the animation plays. You can also delay its start time.<\/p>\n<p><strong>There are 9 properties you can use that allow you to control the CSS <code>animation:<\/code><\/strong><\/p>\n<ul>\n<li>animation-name<\/li>\n<li>animation-duration<\/li>\n<li>animation-iteration-count<\/li>\n<li>animation-direction<\/li>\n<li>animation-timing-function<\/li>\n<li>animation-fill-mode<\/li>\n<li>animation-delay<\/li>\n<li>animation-play-state<\/li>\n<li>animation<\/li>\n<\/ul>\n<p><strong>CSS Example:<\/strong><\/p>\n<div class=\"highlight\">\n<pre>.box {\r\nanimation-name: bounceIn;\r\nanimation-duration: 4s;\r\n}<\/pre>\n<\/div>\n<p>We can shorten it with a SHORTHAND property.<\/p>\n<p>The <strong><code>animation<\/code><\/strong> CSS property is a shorthand property for the various animation properties: <code>animation-name<\/code>, <code>animation-duration<\/code>, \u00a0<code>animation-delay<\/code>,\u00a0<code>animation-direction<\/code>, <code>animation-fill-mode<\/code>, and <code>animation-play-state<\/code>.<\/p>\n<p><strong>Shorthand Example:<\/strong><\/p>\n<div class=\"highlight\">\n<pre>.box {\r\nanimation: bounceIn 4s;\r\n}<\/pre>\n<\/div>\n<p>(Space-separate all the individual values you want to add. The order doesn&#8217;t matter except when using both <strong>duration<\/strong> and <strong>delay<\/strong>. They need to be in that order. <b>Note:<\/b> Always specify the duration, otherwise the duration is 0 and it will never be played.)<\/p>\n<p>REMINDER: value of the\u00a0<code>animation-name<\/code> property must match an identifier in the\u00a0<code>@keyframes<\/code> rule, otherwise no animation will occur.<\/p>\n<p>The word we are matching here is <span class=\"special\">&#8220;bounceIn&#8221;<\/span>.<\/p>\n<h3>Associating The Animation with an HTML Element<\/h3>\n<h4>Next Step<\/h4>\n<p>Next, we need to give an element in our HTML page a class of <strong>.box.<\/strong>\u00a0(See the example below.) It will bounce in and last 4 seconds. We&#8217;ll give another div a class of .box2. It will bounce in twice and last 6 seconds after a 3 second delay. Here&#8217;s the code we need.<\/p>\n<p><strong>CSS Code:<\/strong><\/p>\n<div class=\"highlight\">\n<pre>.box {\r\n animation: bounceIn 4s;\r\n}\r\n\r\n.box2 {\r\nanimation: bounceIn 6s 3s 2;\r\n}\r\n\r\n@keyframes bounceIn {\r\n0% {\r\ntransform: scale(0.1);\r\nopacity: 0;\r\n}\r\n\r\n60% {\r\ntransform: scale(1.2);\r\nopacity: 1;\r\n}\r\n\r\n100% {\r\ntransform: scale(1);\r\n}\r\n}\r\n<\/pre>\n<\/div>\n<div class=\"highlight\">\n<p><strong>HTML Code:<\/strong><\/p>\n<pre><strong>&lt;div class=\"box redBox&gt;<\/strong>...info in our web page...<strong>&lt;\/div&gt;<\/strong>\r\n<strong>&lt;div class=\"box2 blueBox&gt;<\/strong>...info in our web page...<strong>&lt;\/div&gt;<\/strong><\/pre>\n<\/div>\n<p>In our DEMO, we also give the first box a class of .redBox and the second one a class of .blueBox. We&#8217;ve given them different colors, widths etc. in those separate classes.<\/p>\n<p class=\"special\">NOTE: We have not included <a href=\"https:\/\/www.lifewire.com\/css-vendor-prefixes-3466867\" target=\"_blank\" rel=\"noopener\"><strong>CSS vendor prefixes<\/strong><\/a> in the above example for brevity sake.<\/p>\n<p>DEMO:<\/p>\n<p><a class=\"button\" href=\"https:\/\/www.crimsondesigns.com\/blog-examples\/animation-bounce.php\" target=\"_blank\" rel=\"noopener\">Red Box, Blue Box Bounce in<\/a><br \/>\n<a class=\"button\" href=\"https:\/\/www.crimsondesigns.com\/blog-examples\/animation-bounce.css\" target=\"_blank\" rel=\"noopener\">Bounce In Style Sheet\u00a0<\/a><\/p>\n<p>If these examples seem complicated right now, don&#8217;t despair.<\/p>\n<p>Many <strong>code libraries<\/strong> and tools are available online to add CSS animations quickly without having to write the code yourself. <strong><a href=\"https:\/\/daneden.github.io\/animate.css\/\" target=\"_blank\" rel=\"noopener\">Animate.css<\/a><\/strong> provides several pre-made animations you can add to a web page. You can copy all the css code into your own style sheet, then link to the style sheet in the head section of your HTML document.<\/p>\n<p><strong>Here&#8217;s a snippet from the animate.css file that you can download and use:<\/strong><\/p>\n<p>NOTE: We have included a \u00a0<a href=\"https:\/\/www.lifewire.com\/css-vendor-prefixes-3466867\" target=\"_blank\" rel=\"noopener\"><strong>CSS vendor prefix<\/strong><\/a> in this example.<\/p>\n<div class=\"highlight\">\n<pre><span class=\"special\">.subtleFadeIn {\r\n-webkit-animation-name: <strong>fadeInRight;<\/strong>\r\nanimation-name: <strong>fadeInRight;<\/strong>\r\n}\r\n<\/span>\r\n@keyframes <strong>fadeInRight<\/strong> {\r\nfrom {\r\nopacity: 0;\r\n-webkit-transform: translate3d(100%, 0, 0);\r\ntransform: translate3d(100%, 0, 0);\r\n}\r\n\r\nto {\r\nopacity: 1;\r\n-webkit-transform: translate3d(0, 0, 0);\r\ntransform: translate3d(0, 0, 0);\r\n}\r\n}\r\n<\/pre>\n<\/div>\n<p>Notice the <code>@keyframes<\/code>\u00a0rule.\u00a0The @ rule and its identifier <strong>&#8220;fadeInRight&#8221;\u00a0<\/strong>are followed by a number of rule sets (selectors with declaration blocks as in normal CSS code). The rule sets are surrounded by curly braces, which nest the rule sets inside the @ rule.<\/p>\n<p><strong>Follow the Steps<\/strong><\/p>\n<p>In your HTML document, you can give an element, such as a <strong>&lt;div&gt;<\/strong> a class of <strong>.subtleFadeIn<\/strong>, then put the example snippet (above) into a CSS style sheet and use the <strong>&lt;link&gt;<\/strong> tag in your HTML document to link to the style sheet. Then check your page out in a web browser to see the box fade in from the right side.<\/p>\n<p>DEMO:<\/p>\n<p><a class=\"button\" href=\"https:\/\/www.crimsondesigns.com\/blog-examples\/fadeInRight.php\" target=\"_blank\" rel=\"noopener\">fadeInRight DEMO<\/a><br \/>\n<a class=\"button\" href=\"https:\/\/www.crimsondesigns.com\/blog-examples\/fadeInRight.css\" target=\"_blank\" rel=\"noopener\">fadeInRight Style Sheet<\/a><\/p>\n<h3>CSS3 Animations with Javascript (JS)<\/h3>\n<p>JavaScript (and jQuery) allow you to animate things that CSS can\u2019t (such as the scroll position).<\/p>\n<p>Say you want an &#8220;About Us&#8221; section of a page to gradually appear as a visitor scrolls down. A JavaScript component like <a href=\"https:\/\/scrollrevealjs.org\/\">ScrollReveal.js<\/a>\u00a0or <a href=\"https:\/\/mynameismatthieu.com\/WOW\/index.html\" target=\"_blank\" rel=\"noopener\">Wow.js<\/a> can be applied to specific containers (divs) on your page so only those elements animate.<\/p>\n<h3>Scroll-based Animations<\/h3>\n<p>We might want to fade elements in or provide an interesting transformation near the bottom of a long page. We want the animation to occur when the user can actually view them. If we relied on CSS only, the animation would&#8217;ve already taken place before the user reached that section of the page. So we need JavaScript.<\/p>\n<ul>\n<li>JavaScript \u00a0can be added to any web page using a <strong>&lt;<a class=\"acode\" href=\"http:\/\/www.htmldog.com\/references\/html\/tags\/script\/\"><code>script<\/code><\/a>&gt;<\/strong> tag.<\/li>\n<li>The <strong>&lt;script&gt;<\/strong> tag can either contain JavaScript directly (<strong>internal<\/strong>) or link to an external file via the\u00a0<code>src<\/code>attribute (<strong>external<\/strong>).<\/li>\n<li>An external JavaScript file is a <strong>text file<\/strong> with a <strong>.js<\/strong> extension.<\/li>\n<li>You can attach multiple external JavaScript files to a single web page.<\/li>\n<\/ul>\n<h2 id=\"adding-javascript-into-an-html-document\">Adding JavaScript into an HTML Document<\/h2>\n<h3><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5764\" src=\"https:\/\/www.crimsondesigns.com\/blog\/wp-content\/uploads\/2018\/07\/javascript-script-tag.png\" alt=\"\" width=\"300\" height=\"83\" \/><\/h3>\n<p>The <strong><code>&lt;script&gt;<\/code><\/strong> tag can be placed in the <strong><code>&lt;head&gt;<\/code><\/strong> section of your HTML, in the\u00a0<strong><code>&lt;body&gt;<\/code><\/strong> section or after the <strong><code>&lt;\/body&gt;<\/code><\/strong> close tag.\u00a0Scripts that are small can be put in an HTML file.\u00a0Larger scripts or those that will be used across several pages, are generally placed in a <strong><code>js<\/code><\/strong> file that is referenced by the HTML document.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"code-pre \"><code class=\"code-highlight language-html hljs \"><span class=\"highlight\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">script<\/span> <span class=\"hljs-attribute\">src<\/span>=<span class=\"hljs-value\">\"js\/script.js\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-title\">script<\/span>&gt;<\/span><\/span><\/code><\/pre>\n<p>The <strong><code>&lt;script&gt;<\/code><\/strong> tag in this example is pointing to the <code>script.js<\/code> file in the <code>js<\/code> folder of our website.<\/p>\n<p>It&#8217;s usually a good idea to put the &lt;script&gt; element near the bottom of the HTML file for better PAGE SPEED.<\/p>\n<h4>JavaScript Libraries<\/h4>\n<p>There are collections of JavaScript code called libraries, which provide useful scripts that you can use. These libraries often make it easy to do something that\u2019 quite difficult. Copy and paste the code into your own JS file and add it to your website. Then use the <strong>&lt;script&gt;<\/strong> tag in your web page to link to it.<\/p>\n<p>Want to add subtle animations that take place as a user scrolls down your web page? Try adding Javascript from\u00a0<strong><a href=\"https:\/\/mynameismatthieu.com\/WOW\/index.html\" target=\"_blank\" rel=\"noopener\">Wow.js<\/a><\/strong>\u00a0or \u00a0<strong><a href=\"https:\/\/scrollrevealjs.org\/\" target=\"_blank\" rel=\"noopener\">ScrollReveal.js<\/a><\/strong> along with CSS code from\u00a0<strong><a href=\"https:\/\/daneden.github.io\/animate.css\/\" target=\"_blank\" rel=\"noopener\">Animate.css<\/a>.<\/strong><\/p>\n<p><strong>Script Tag Example:<\/strong><\/p>\n<div class=\"highlight\">\n<pre>&lt;script src=\"js\/wow.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\nnew WOW().init();\r\n&lt;\/script&gt;\r\n<\/pre>\n<\/div>\n<p>One set of script tags in this example links to an external javascript file (wow.min.js) that is in a &#8220;<strong>js<\/strong>&#8221; folder. (This script is <strong>minified<\/strong> to make it compact so it loads faster.)<\/p>\n<p>The second set of script tags, in the example, surrounds code that <strong>initiates<\/strong> the external script.<\/p>\n<p>IMPORTANT: An element in the web page must have a class of <strong>.wow<\/strong> to reference the &#8220;wow&#8221; js file and a class of <strong>.fadeInRight<\/strong> that references one of the styles in animate.css file. If you miss the .wow part, you&#8217;re animation will be triggered right away and not wait for you to scroll down the page.<\/p>\n<div class=\"highlight\">\n<p><strong>HTML Example:<\/strong><\/p>\n<pre>&lt;div class=\"wow fadeInRight\"&gt;\r\nContent to Fade In when User Scrolls down a Web Page\r\n&lt;\/div&gt;\r\n<\/pre>\n<\/div>\n<p>DEMO:<\/p>\n<p><a class=\"button\" href=\"https:\/\/www.crimsondesigns.com\/blog-examples\/fade-in-scroll.php\" target=\"_blank\" rel=\"noopener\">Fade In on Scroll DEMO Animation<\/a><br \/>\n<a class=\"button\" href=\"https:\/\/www.crimsondesigns.com\/blog-examples\/fade-in-scroll.css\" target=\"_blank\" rel=\"noopener\">Fade In on Scroll DEMO Style Sheet<\/a><\/p>\n<p>SUMMARY: CSS3 and JavaScript can be used to create subtle animations for a web page. We will use a <strong>&lt;link&gt;<\/strong> tag in our HTML page to reference our CSS file and a <strong>&lt;script&gt;<\/strong> tag to reference our JavaScript file. We will use CSS rules in our style sheet and SHORTHAND properties to keep our CSS code concise. We will match our &#8220;selectors&#8221; in our style sheet with class names in our HTML elements. Then we will experiment with transition, transform, @keyframes and animation properties. We will pay attention to the number of animations and speed of them. By adding them sparingly and at speeds that won&#8217;t annoy our visitors, we can create subtle animations that will improve user experience.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Beginning CSS3 animation CSS3 gives us two primary ways of animating HTML elements: &#8220;transition&#8221;\u00a0and\u00a0&#8220;transform&#8221; manipulate elements from one state to another &#8220;CSS animations&#8221;\u00a0can control a complex series of movements during an animation 1) CSS Transitions\u00a0are used for creating a smooth transition from one state to another. Transitions specify the duration of an element. Without a<a href=\"https:\/\/www.crimsondesigns.com\/blog\/subtle-animations-css-javascript-part-2\/\" class=\"read-more\">&nbsp; Continue Reading &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":6018,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[304,115],"class_list":{"0":"post-7227","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-html-and-css","8":"tag-css-animation","9":"tag-javascript","10":"cat-5-id","11":"has_thumb"},"_links":{"self":[{"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/posts\/7227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=7227"}],"version-history":[{"count":0,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/posts\/7227\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/media\/6018"}],"wp:attachment":[{"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=7227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=7227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.crimsondesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=7227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}