If you are a designer the chances are you will have an online presence, therefore you will need somewhere to showcase your work to the world. What better way than adding a slideshow of a selection of your best artwork that demonstrates your talents. Slideshows are nothing new and have been around a while, but is becoming a popular choice among high profile sites, as it’s a great user-friendly way to display images and information without disrupting the user experience.
In this tutorial we are going to show you the basics of setting up your own slideshow of images that you can place on your homepage or on your portfolio page. Throughout this tutorial feel free to skip any parts you have past knowledge on as we are writing this from the bottom up.
First of let’s start with our basic HTML5 document. HTML5 is the latest version of the HTML standard and is a way of structuring information online.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
</html>
Next lets add a page title, create our placeholder for our slideshow so we can hook our JavaScript call to the particular unique ID on the page and start adding in our images. For clarity we are surrounding each image within a div element. This will allow our JavaScript slider to determine one slide to the next and will give us the option later to add additional content, for example, a call to action. You will also notice that all images have the same width and height. Unlike an image light box where it can be flexible (as it floats above all page content at full screen) a slideshow should remain the same size throughout. Having images at different sizes will cause your page to bounce and if you set the slider to change automatically this would be a frustrating experience for your user.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Slideshow Demo</title> </head> <body> <div id="slideshow"> <div> <img src="images/digital-art.png" width="960" height="250" alt="Abstract digital art" /> </div> <div> <img src="images/logo-design.png" width="960" height="250" alt="Logo design" /> </div> <div> <img src="images/t-shirt-design.png" width="960" height="250" alt="T-shirt design" /> </div> </div> </body> </html>
Now we have the images in place lets add some page styling ready for the JavaScript slideshow. First add the CSS link into the head. This should point to your CSS file so we can style the HTML document.
<head> <meta charset="utf-8"> <title>Slideshow Demo</title> <link href="styles.css" rel="stylesheet" type="text/css"> </head>
Next, create your styles.css file and lets add some properties. This is very basic styling, but you can modify these to your needs. One thing to note is that it’s a good idea to add “overflow:hidden;” to your main slider div. This will ensure that you don’t see the transitions outside our slide box.
body{width:960px;margin:20px auto 0;}
#slideshow{width:960px;overflow:hidden;}
At this stage you should have two files. One CSS file named styles.css and the HTML5 document. Next lets add the jQuery and slideshow libraries. As HTML renders the document from top to bottom, it’s important to add our jQuery first, as our jQuery slideshow depends on the jQuery library already loaded. For speed and availability we are using a CDN (Content delivery network) to load both libraries. There are many good reasons to use a CDN compared to hosting the files yourself, but for the purpose of this tutorial it will ensure you have the correct files in order to get your slideshow working.
<head> <title>Slideshow Demo</title> <link href="styles.css" rel="stylesheet" type="text/css"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script src="http://ajax.aspnetcdn.com/ajax/jquery.cycle/2.99/jquery.cycle.all.min.js"></script></script> </head>
At this stage we have everything in place, next we just need to tell jQuery cycle which element on the page is our slideshow and it will then take care of the rest. There are many options available to us via the jQuery cycle plug-in but for this tutorial, we are using a simple scroll left effect, with a 5.5 second timer between transitions which when hovered over with the mouse will pause the slider. Also we are forcing the slides to fit the container by setting the fit to ‘1’. Lets put our code at the bottom of the document. The reason for this is because it lets the rest of the page load first, which if there is an issue with the JavaScript or jQuery it won’t halt the page load/display. This is common practice to keep JavaScript at the bottom of the page.
<script>
$("#slideshow").cycle({fx:"scrollLeft",timeout:5500,fit:1,height:250,width:960,pause:1});
</script>
Now you save and refresh your page your slider should work. But there is potentially an issue with the above. As the page loads it’s very likely that jQuery might not have fully downloaded. If we try and make a jQuery call without jQuery present, JavaScript errors will occur. So to combat this let’s put in a document ready function, which means the JavaScript code won’t run until the document (including linked files) have been fully downloaded.
<script>
$(function(){
$("#slideshow").cycle({fx:"scrollLeft",timeout:5500,fit:1,height:250,width:960,pause:1});
});
</script>
Now give your page a save and refresh and that’s it! Now you can copy and paste this snippet of code into your site, or save it for later and add to a site your currently designing. You can read more about jQuery and jQuery cycle for more demos and features.
