{"id":1010,"date":"2010-09-15T10:12:26","date_gmt":"2010-09-15T09:12:26","guid":{"rendered":"http:\/\/www.simonbattersby.com\/blog\/slide-images\/"},"modified":"2013-09-16T15:36:27","modified_gmt":"2013-09-16T14:36:27","slug":"simple-jquery-image-slider","status":"publish","type":"page","link":"https:\/\/www.simonbattersby.com\/blog\/simple-jquery-image-slider\/","title":{"rendered":"Simple jQuery image slider"},"content":{"rendered":"<p>Bored this morning so I thought I would adapt my <a href=\"\/blog\/simple-jquery-image-crossfade\/\" title=\"jQuery crossfade image cycler\">jQuery image crossfade cycler<\/a> into an image slider. Tested in IE6\/7\/8, Firefox 3, Chrome, Opera 9\/10, Safari 4, all on Windows XP, and Safari 5 on Mac OS X\/Snow Leopard. You might also want to have a look at my <a href=\"\/blog\/jquery-image-and-caption-slider\/\" title=\"Image and caption slider\">image and caption slider demo<\/a>.<\/p>\r\n\r\n<h3>The demo<\/h3>\r\n\r\n<div id=\"portfolio_slider\" class=\"shadow roundbox\">\r\n<div id=\"slider_box\">\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\"  \/>\r\n<\/div>\r\n<\/div>\r\n\r\n<h3>The code<\/h3>\r\n\r\n<p>Here&#8217;s the necessary css &#8211; just to show the important bits &#8211; you&#8217;ll need to add some more to position and style as required. Depending on your layout you may also need to set the height and width on #porfolio_cycler to match your images.<\/p>\r\n<pre>\r\n#portfolio_slider{position:relative;overflow:hidden}\r\n#portfolio_slider img{position:absolute;left:100%}\r\n#portfolio_slider img.active{left:0}\r\n<\/pre>\r\n\r\n<p>And here&#8217;s the javascript:<\/p>\r\n\r\n<pre>function slideImages(){\r\n      var $active = $('#portfolio_slider .active');\r\n      var $next = ($('#portfolio_slider .active').next().length &gt; 0) ? $('#portfolio_slider .active').next() : $('#portfolio_slider img:first');\r\n      $next.css('z-index',2);<span class=\"code_comment\">\/\/set the z-index so the image will slide across the top of  current one\r\n<\/span>      $active.css('z-index',1);<span class=\"code_comment\"><span class=\"code_comment\">\/\/and set the current active image so it's below<\/span>&gt; \r\n<\/span>      $next.animate({left:0},\"slow\",function(){<span class=\"code_comment\">\/\/slide in the next image\r\n<\/span>              $active.removeClass('active').css('left',\"100%\");<span class=\"code_comment\">\/\/when the slide's finished, reset the previous active image\r\n<\/span>              $next.addClass('active');<span class=\"code_comment\">\/\/and set the new image to active\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('slideImages()', 7000);\r\n    })<\/pre>\r\n\r\n\r\n\r\n<h3>The logic<\/h3>\r\n<p>The slider div is set with overflow:hidden. All the images are loaded with the page and positioned absolutely within the slider div, with <code>left:506px<\/code> which is the width of my slider, so the images aren&#8217;t visible. One image is initially set with a class of &#8220;active&#8221; which sets <code>left:0<\/code> , so that it is visible.<\/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 image, and <code>z-index=1<\/code> to the active image, so that the next image will slide across the top of the active image.<\/p>\r\n\r\n<p>Then, I&#8217;m animating the next image using its <code>left<\/code> property. When this completes I&#8217;m removing the class of the previously active image and moving it back outside the slider so it&#8217;s ready to slide in again, and I&#8217;m setting the new image to active.<\/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>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<script type=\"text\/javascript\" src=\"\/javascript\/slide_images.js\"><\/script>","protected":false},"excerpt":{"rendered":"<p>Bored this morning so I thought I would adapt my jQuery image crossfade cycler into an image slider. Tested in IE6\/7\/8, Firefox 3, Chrome, Opera 9\/10, Safari 4, all on Windows XP, and Safari 5 on Mac OS X\/Snow Leopard. You might also want to have a look at my image and caption slider demo. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1010","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/1010","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=1010"}],"version-history":[{"count":4,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/1010\/revisions"}],"predecessor-version":[{"id":2885,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/1010\/revisions\/2885"}],"wp:attachment":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/media?parent=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}