How to switch to a slideshow view via a simple click using jQuery

Scrolling down to view a complete list of photos can be very annoying, but on the other hand, some people don’t like slideshows and prefer the scrolling solution. There is a way to keep everyone happy: Just give them the option to switch between the two views easily. I’m going to show you one way to build such a solution.

Before I start diving into the code, I’ll explain what we will build. If you are impatient, you can play around with the demo and/or download the source code. We will start by having a regular list of photographs. You can scroll down to view them all as usual. The difference is that at the top of the page there is going to be a button that will allow you to switch to the slideshow view without having to use AJAX or load another page.

When you click ον it, all the photos (except from the first one) will disappear and you will be able to navigate through them either by using your keyboard’s left & right arrow keys or by clicking the left & right navigation buttons that will appear if you hover over the photo. Have a look at the demo if you haven’t done already and then let’s start building it.

First of all, let’s talk html:

We only need a simple button that will be used to switch between the views and a simple unordered list to hold our photos:

Before we continue, please note 2 things:

  1. The slideshow effect can work with any content; for example, you can use only text or a combination of text and images.
  2. In the demo & the source code I’m using some markup & CSS only to make things look better; all the important stuff are explained in the tutorial.

Now, let’s go to the JavaScript code. All of the JS code will be included into a $(document).ready function. We will keep the selection of the view (either a list which is the default view or a slideshow) in a variable called mode, and we will deal with what happens when the user clicks the button to switch views:

Initially, if the current mode is set to “list”, we change it to “slideshow” and we begin by setting the value of the display CSS property to none for all the list items. Then, we select the first list item (i.e. the first photo), we reset its display value so it becomes visible and we add 2 classes to it, called active and first, which will be used later. We also add a class called last to the last element of the list. Finally, we change the text of the button and we add a paragraph containing a tip to the page.

If the user clicks the button again (which now indicates he’ll be back to the list mode), we change the mode value back to list, we make all the list items visible, remove all the classes that we added along with the tip and finally we reset the text of the button to its default value.

Our tip indicated that we can use our left & right arrow keys to navigate through the slideshow, so let’s build that next. The code to monitor if a key is pressed, identify its value and do something when this happens, is plain simple:

As you can imagine, functions moveLeft() and moveRight() does all the hard work:

I’ll first explain what moveRight() does: If the list item’s class is last, it indicates that it’s the last item of the list, therefore nothing happens. If it’s not, then the following sequence is executed:

  1. The active list item’s (indicated by the active class) display value is set to none.
  2. The item which is next in the list is selected and it becomes visible.
  3. The active class is removed from the currently active item and is transferred to the next one (the one that just became visible).

moveLeft() is working with the same principles, with 2 differences: Nothing happens if we’re at the first item of the list and we go backwards (the list item that becomes visible is the previous one).

All the mechanics that will allow the slideshow to work are here, but the navigation works only via the keyboard. Therefore, let’s add some mouse-clickable navigation as well:

Using the above code, we add two anchor tags which represent a right and a left arrow so the user can navigate using the mouse. The addHoverAndClickEffect() function binds the click and hover event handlers to the anchor tags so they become clickable and visible when the user hovers over the photo.

That’s all! You can download the source code and view the demo if you wish.

Update on 22nd of February, 2014: I created a second version of this tutorial, adding a fade effect between the image changes, based on a reader’s question. Check out the demo.

PS: The comic strips used in the tutorial belong to the amazing PHD Comics.

Photo by fwickafwee