{"id":4883,"date":"2013-07-19T02:00:00","date_gmt":"2013-07-19T00:00:00","guid":{"rendered":"https:\/\/alvarotrigo.com\/epa\/creating-a-jquery-animation-from-a-single-sprite-image\/"},"modified":"2024-02-08T00:28:43","modified_gmt":"2024-02-07T23:28:43","slug":"creating-a-jquery-animation-from-a-single-sprite-image","status":"publish","type":"post","link":"https:\/\/alvarotrigo.com\/blog\/creating-a-jquery-animation-from-a-single-sprite-image\/","title":{"rendered":"Creating a jQuery animation from a single sprite image"},"content":{"rendered":"\n<p>Years ago it was quite common to see GIF animations all over the net. Basically we call GIF images to any image using the .gif extension. The Gif format provides a way to display a sequence of images one after another creating the illusion of an animation, same as the way cartoon movies were made in the past.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-src=\"https:\/\/alvarotrigo.com\/blog\/wp-content\/uploads\/2013\/07\/jquery-sprite-animation.png\" alt=\"javascript sprite image\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 657px; --smush-placeholder-aspect-ratio: 657\/300;\" \/><\/figure>\n\n\n<h2 class=\"wp-block-heading\" id=\"old-days\">Old days<\/h2>\n\n\n<p>Currently the use of Gif animations in websites has dramatically decreased, but why? Although you can actually have GIF animations with end (no loop), the fact that the color palette is so reduced (up to 256 per frame) has taken an important role on it. Color graduations were not looking good at all and many transparencies were being affected by this restriction.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"the-modern-way\">The modern way<\/h2>\n\n\n<p>We still want to show some animation in our websites from time to time. For some, we take courage and make use of CSS3 animations (and hopefully provide some fallback for non css3 browsers&#8230;)<\/p>\n\n\n\n<p>But we can also go for another option that provides us with the freedom of using images for our animations with all this implies. (remember the cartoons movies? )<br>\nWith just the use of Javascript we can have a pretty decent &#8220;movie&#8221;.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"a-simple-and-quick-solution\">A simple and quick solution<\/h2>\n\n\n<p>First, design your sequence of images. Remember now we will be able to use any image format (super quality images!). And please, <strong>do not use GIF format here or you won&#8217;t gain anything from this! \ud83d\ude42<\/strong><\/p>\n\n\n\n<p>So, basically we will be creating a simple sprite of images and then using jQuery move the position of the image inside a container:<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"javascript\">Javascript<\/h2>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">var<\/span> imgHeight = <span class=\"hljs-number\">360<\/span>;\n<span class=\"hljs-keyword\">var<\/span> numImgs = <span class=\"hljs-number\">67<\/span>;\n<span class=\"hljs-keyword\">var<\/span> cont = <span class=\"hljs-number\">0<\/span>;\n<span class=\"hljs-keyword\">var<\/span> img = $(<span class=\"hljs-string\">'#container'<\/span>).find(<span class=\"hljs-string\">'img'<\/span>);\n\n<span class=\"hljs-keyword\">var<\/span> animation = setInterval( moveSprite,<span class=\"hljs-number\">100<\/span>);\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">moveSprite<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>{\n    img.css(<span class=\"hljs-string\">'margin-top'<\/span>, <span class=\"hljs-number\">-1<\/span> * (cont*imgHeight));\n\n    cont++;\n    <span class=\"hljs-keyword\">if<\/span>(cont == numImgs){\n        clearInterval(animation);\n    }\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"html\">HTML<\/h2>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">img<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/d3ckw5pamzrtgd.cloudfront.net\/assets\/animations\/care\/animation.png\"<\/span> <span class=\"hljs-attr\">alt<\/span>=<span class=\"hljs-string\">\"My super animation\"<\/span>\/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"css\">CSS<\/h2>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-id\">#container<\/span>{\n    <span class=\"hljs-attribute\">width<\/span>:<span class=\"hljs-number\">100%<\/span>;\n    <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">350px<\/span>;\n    <span class=\"hljs-attribute\">display<\/span>:block;\n    <span class=\"hljs-attribute\">overflow<\/span>:hidden;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/alvarotrigo.com\/blog\/demos\/spriteAnimation.php\" title=\"See Demo\" target=\"_blank\" rel=\"noopener\">See Demo<\/a><\/div>\n<\/div>\n\n\n\n<p>Here&#8217;s <a href=\"http:\/\/jsfiddle.net\/aTqGw\/1\/\" target=\"_blank\" rel=\"noopener nofollow\">a fiddle<\/a> in case you want to play a bit.<\/p>\n\n\n\n<p>The sprite image I used for the example was taken from http:\/\/onlinedepartment.nl\/.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Years ago it was quite common to see GIF animations all over the net. Basically we call GIF images to any image using the .gif extension. The Gif format provides a way to display a sequence of images one after another creating the illusion of an animation, same as the way cartoon movies were made in the past.<\/p>\n","protected":false},"author":1,"featured_media":4882,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[120],"tags":[9,10,18],"class_list":["post-4883","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery","tag-css","tag-javascript","tag-jquery"],"acf":[],"_links":{"self":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/4883","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/comments?post=4883"}],"version-history":[{"count":4,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/4883\/revisions"}],"predecessor-version":[{"id":9594,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/4883\/revisions\/9594"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media\/4882"}],"wp:attachment":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media?parent=4883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/categories?post=4883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/tags?post=4883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}