{"id":803,"date":"2010-07-26T10:55:23","date_gmt":"2010-07-26T09:55:23","guid":{"rendered":"http:\/\/www.simonbattersby.com\/blog\/?page_id=803"},"modified":"2026-05-17T16:34:38","modified_gmt":"2026-05-17T15:34:38","slug":"simple-jquery-image-crossfade","status":"publish","type":"page","link":"https:\/\/www.simonbattersby.com\/blog\/simple-jquery-image-crossfade\/","title":{"rendered":"Simple jQuery image crossfade"},"content":{"rendered":"<p>I wanted a simple jQuery crossfade effect for an image cycler last week, and surprisingly couldn&#8217;t find anything suitable. So I ended up writing my own, and here it is. Tested in IE6\/7\/8, Firefox 3, Chrome, Opera 9\/10, Safari 4, all on Windows XP, IE8\/9, Firefox, Chrome and Safari 5 on Win 7, Safari 5 on Mac OS X\/Snow Leopard, and Android Browser and Dolphin on Android.<\/p>\r\n\r\n<h3>The demo<\/h3>\r\n\r\n<div id=\"portfolio_cycler\" class=\"shadow roundbox\">\r\n<img loading=\"lazy\" decoding=\"async\" class=\"active\" src=\"\/images\/photography\/insects\/Gallery1.jpg\" alt=\"Hoverfly on a green leaf\" title=\"Hoverfly on a green leaf\" width=\"506\" height=\"337\" \/>\r\n<img loading=\"lazy\" decoding=\"async\" src=\"\/images\/photography\/insects\/Gallery11.jpg\" alt=\"Fly on a pink flower\" title=\"Fly on a pink flower\" width=\"506\" height=\"337\"  \/>\r\n<img loading=\"lazy\" decoding=\"async\" src=\"\/images\/photography\/insects\/Gallery10.jpg\" alt=\"Insect on a leaf\" title=\"Platycheirus granditarsus (I think) on a leaf\" width=\"506\" height=\"337\"  \/>\r\n<img loading=\"lazy\" decoding=\"async\" src=\"\/images\/photography\/insects\/Gallery12.jpg\" alt=\"Fly\" title=\"Blue bottle fly on a green leaf\" width=\"506\" height=\"337\"  \/>\t\t\r\n<\/div>\r\n\r\n<p>This <a href=\"\/demos\/crossfade_demo_basic.htm\">basic demo page<\/a> will show you exactly what&#8217;s needed.<\/p>\r\n\r\n<h3>The code<\/h3>\r\n\r\n<p>You need the following basic html:<\/p>\r\n\r\n<pre>&lt;div id=\"cycler\"&gt;\r\n&lt;img class=\"active\" src=\"image1.jpg\" alt=\"My image\" \/&gt;\r\n&lt;img src=\"image2.jpg\" alt=\"My image\" \/&gt;\t\r\n&lt;img src=\"image3.jpg\" alt=\"My image\" \/&gt;\t\r\n&lt;img src=\"image4.jpg\" alt=\"My image\" \/&gt;\t\t\r\n&lt;\/div&gt;<\/pre>\r\n\r\n<p>Here&#8217;s the necessary css &#8211; just to show the z-index and positioning &#8211; you&#8217;ll need to add some more to position as required. Depending on your layout you may also need to set the height and width on #cycler to match your images:<\/p>\r\n\r\n<pre>\r\n#cycler{position:relative;}\r\n#cycler img{position:absolute;z-index:1}\r\n#cycler img.active{z-index:3}\r\n<\/pre>\r\n\r\n<p>And here&#8217;s the javascript:<\/p>\r\n\r\n<pre>function cycleImages(){\r\n      var $active = $('#cycler .active');\r\n      var $next = ($active.next().length &gt; 0) ? $active.next() : $('#cycler img:first');\r\n      $next.css('z-index',2);<span class=\"code_comment\">\/\/move the next image up the pile\r\n<\/span>      $active.fadeOut(1500,function(){<span class=\"code_comment\">\/\/fade out the top image\r\n<\/span>\t  $active.css('z-index',1).show().removeClass('active');<span class=\"code_comment\">\/\/reset the z-index and unhide the image\r\n<\/span>          $next.css('z-index',3).addClass('active');<span class=\"code_comment\">\/\/make the next image the top one\r\n<\/span>      });\r\n    }\r\n\r\n$(document).ready(function(){\r\n<span class=\"code_comment\">\/\/ run every 7s\r\n<\/span>setInterval('cycleImages()', 7000);\r\n})<\/pre>\r\n\r\n<h3>The logic<\/h3>\r\n<p>All the images are loaded with the page and positioned absolutely on top of each other, all with <code>z-index=1<\/code>. One image is initially set with a class of &#8220;active&#8221;, which is positioned on the top of the stack with <code>z-index = 3<\/code>.<\/p> \r\n\r\n<p>Then, using jQuery, I&#8217;m identifying the active image, and then the next image down. I&#8217;m doing this via the jQuery <code>.next()<\/code> method. If there isn&#8217;t a next image &#8211; if we&#8217;re at the end of the images &#8211; I&#8217;m selecting the first image instead using <code>.first()<\/code>. <\/p>\r\n\r\n<p>Now, once I&#8217;ve identified the next image, I&#8217;m assigning <code>z-index=2<\/code> to this so it&#8217;s the second image down in the pile &#8211; and hence is visible when the top image fades out.<\/p>\r\n\r\n<p>Then, I&#8217;m fading out the active image. When this completes there&#8217;s a few things to do. First, I&#8217;m setting the z-index of the active image (now faded out) back to <code>z-index=1<\/code>. Then, I&#8217;m reversing the fade by using the <code>show()<\/code> method. It won&#8217;t be visible now because of the z-index. Finally, I&#8217;m removing the active class. Effectively, I&#8217;m putting this image back into the bottom pile.<\/p>\r\n\r\n<p>Nearly there. Then, with the next image, which is now on top anyway, I&#8217;m assigning <code>z-index=3<\/code>, and then adding the active class to this image. <\/p>\r\n\r\n<p>Finally, all of this code is sitting in a function which I&#8217;m calling every seven seconds via <code>setInterval()<\/code>.<\/p>\r\n<h3>Responsive version<\/h3>\r\n<p>Making this responsive (without media queries, which is what this page uses) is pretty striaghtforward. The trick is to use a  &#8216;base&#8217; image which is not absolutely positioned to set the dimensions of the cycler and then apply a max-width to #cycler. The base image is then cloned and the cloned version is faded in and out. Have a look at the <a href=\"\/demos\/crossfade_demo_basic_responsive.htm\">responsive demo here<\/a>.<\/p>\r\n<h3>Help! It doesn&#8217;t work<\/h3>\r\n<p>If you hit problems, start off by looking at my <a href=\"\/blog\/debugging-jquery-a-beginners-guide\/\" title=\"Debugging jQuery \u2013 a beginner\u2019s guide\">beginners&#8217; guide to debugging jquery<\/a>. Most of the problems encountered by users are listed here.<\/p>\r\n\r\n<h3>Can I use this on my site?<\/h3>\r\n<p>Probably. See my <a href=\"\/blog\/use-of-code-from-this-site\/\">policies on using code from this site<\/a>.<\/p>\r\n\r\n<h3>Variations on the basic crossfade<\/h3>\r\n<p>Here are a few variations, mostly built in response to questions or comments on the basic code. View the source to see how they&#8217;re done.<\/p>\r\n<p><a href=\"\/demos\/crossfade_demo_cycle_once.htm\">Cycle through images once and then stop<\/a><\/p>\r\n<p><a href=\"\/demos\/crossfade_demo_multi.htm\">Multiple cyclers on a page (with a common periodicity)<\/a><\/p>\r\n<p><a href=\"\/demos\/crossfade_demo_multi_2.htm\">Multiple cyclers on a page (with different periodicity)<\/a><\/p>\r\n<p><a href=\"\/demos\/crossfade_demo_controls.htm\">Multiple cyclers with slideshow controls<\/a><\/p>\r\n<p><a href=\"\/demos\/crossfade_demo_variable_times.htm\">Cycler with variable timings<\/a><\/p>\r\n<p><a href=\"\/demos\/crossfade_demo_cycle_divs.htm\">Cycle divs instead of images<\/a><\/p>\r\n<p><a href=\"\/demos\/crossfade_demo_cycle_superimposed_text.htm\">Superimpose text on the cycler<\/a><\/p>\r\n<p><a href=\"\/demos\/crossfade_demo_fadeout.htm\">Cycler with fadeout transition<\/a><\/p>\r\n<p><a href=\"\/demos\/crossfade_demo_no_slideshow.htm\">Crossfade images, no slideshow<\/a><\/p>\r\n<p><a href=\"\/demos\/crossfade_demo_no_slideshow_thumbnails.htm\">Gallery with thumbnails<\/a><\/p>\r\n\r\n<script type=\"text\/javascript\" src=\"\/javascript\/crossfade_images.js\"><\/script>","protected":false},"excerpt":{"rendered":"<p>I wanted a simple jQuery crossfade effect for an image cycler last week, and surprisingly couldn&#8217;t find anything suitable. So I ended up writing my own, and here it is. Tested in IE6\/7\/8, Firefox 3, Chrome, Opera 9\/10, Safari 4, all on Windows XP, IE8\/9, Firefox, Chrome and Safari 5 on Win 7, Safari 5 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-803","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/comments?post=803"}],"version-history":[{"count":23,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/803\/revisions"}],"predecessor-version":[{"id":2587,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/803\/revisions\/2587"}],"wp:attachment":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/media?parent=803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}