Project: Create your own Image Carousel Slider with jQuery

Create your own Image Carousel Slider with pure jQuery. Its not only about the slider, you will learn how a Front-End Developer thinks creatively to build a web feature

PAIDLevel: Intermediate27:07 mins
Project: Create your own Image Carousel Slider with jQuery
You do not have access to this lesson!

Enroll in the training to get access to this lesson along with hundreds of other lessons and complete courses.

Already enrolled? Login

Course content
Lessons #1: Getting Started with jQuery PAID
10:21 mins
Lessons #2: Find Something / Query the DOM with jQuery - part 1 PAID
12:52 mins
Lessons #3: Find Something / Query the DOM with jQuery - part 2 PAID
22:25 mins
Lessons #4: Find Something / Query the DOM with jQuery - part 3 PAID
14:24 mins
Lessons #5: Do Something with jQuery PAID
11:39 mins
Lessons #6: Events with jQuery PAID
19:47 mins
Lessons #7: Project: Create a Top Bar Widget with jQuery PAID
23:47 mins
Lessons #8: Project: Create Tabs functionality with jQuery PAID
10:46 mins
Lessons #9: Animate: Animating elements with jQuery PAID
9:41 mins
Lessons #10: Event Delegation with jQuery PAID
4:52 mins
Lessons #11: Event Propagation with jQuery PAID
6:20 mins
Lessons #12: Prevent Default Behavior (of elements) with jQuery PAID
3:06 mins
Lessons #13: Content Manipulation with jQuery PAID
9:32 mins
Lessons #14: Keyboard Event with jQuery PAID
7:13 mins
Lessons #15: Project: Create your own Image Carousel Slider with jQuery PAID
27:07 mins
Lessons #16: Ajax: Create a Single Page App with jQuery PAID
15:09 mins
Lessons #17: Ajax: Create a Location Finder App using jQuery & Google Maps API - Part 1 PAID
9:53 mins
Lessons #18: Ajax: Create a Location Finder App using jQuery & Google Maps API - Part 2 PAID
4:18 mins

Create your own Image Carousel Slider with jQuery

In this lesson, we are going to create an Image Carousel Slider with jQuery which has an image gallery and when you hit the next button, the next photo shows and so on as shown below


Our HTML Document

Below is how our HTML document looks, you see I have imported the jQuery, I have created a script.js file and also linked in the HTML document. I have linked the styles sheet as well.

In our body, I have two divs. One with a class of main-class and another with a class of header as shown below


Style sheet
This is how our style sheet looks like, I have some basic styling implemented just so the slider looks beautiful

 
If I open the project in the browser, this is how our page looks like 

 
In our HTML docment I'm going to create some divs so I can import our images. I'll have a div with a class of slider and inside I'll have the ul tag and three li's inside it like such


Save it, refresh the screen and this is how our page looks now. It has three images imported on the slider


Now we need to style our slider so the list item doesn't show up right before the image. Also make it like such so it only shows one image at a time.
I'm going to remove the bullets and float it to left as shown below

.slider ul {
  list-style: none;
}
.slider ul li {
  float: left;
}


Save it, refresh the screen and you see the bullets are not removed and its floated on the left hand side


I like writing styles in Chrome Developer Tools because the impact is immediate and I can tell how it exactly look, and once we're comfortable with whatever the design looks good in the Chrome developer tools we can simply copy and paste the styles in the style sheet. but again it's personal preference.



I'm going to change the width of the images to 500px each so 500 x 3 is 1500px. You see immediately the moment I change the styles, the images are not side by side.



I'm going to write some css and paste it in the css file

.slider ul {
  list-style: none; width: 1500px; position: relative; left: -1000px; padding: 0; margin: 0;
}
.slider ul {
  width: 500px;
  overflow: hidden;
}



Save it, refresh the screen and you see the slider looks much better now


I'm also going to have a div with a class of nav and inside that, I'm going to have two buttons like such



Save it, refresh the page and you see the slider has now two buttons


But you notice our main class, the white area around the slider is so big and it's looking ugly. I'm going to change the width of the main-class to 500px like such



Save it, refresh the screen and you see it looks much better now


Now we are going to write some jQuery in our script.js file and query the dom as shown below, also console.log(whichButton); just for demonstration purposes

 $(document).ready(function($){
   $('.nav button').on('click', function(){ 
   var whichButton = $(this).data('nav'); 
   console.log(whichButton);
   });
});
Save it, refresh the screen and now if I click on the next button or the previous button, it will display in the console, which button was clicked as shown below 



Save it, refresh the screen and if I click the buttons you can see it in the console that two buttons were clicked


Also did you notice that the 3rd image is showing in the slider right now, if you want the first image to be your default image you can change the default left value to and the width of the images should be dynamically calculated based on the number of images it has in it. For example we have 10 images and 500px width of image x times 10 = 5000px so this is why the width has to be 5000px so all the ten images can be stacked side by side next to the first, second, third and till 10. But in our case we have only 3 images which is 500px x 3 = 1500px this is why we have 1500px. But our default width should be 500px and that should be changed on the next fly when the slider loads



I'm going to create a variable and called imageWidth go to the slider and go to the ul tag and inside the ul tag, go to the li tag and since we have two li tags inside our html document I can say get  the first children which has the tag of img and then get its width

$(document).ready(function($){
  var imageWidth = $('.slider ul li').first().children('img').width(); console.log(imageWidth);
});


Save it, refresh the page and you see in the console, the width has been calculated


Now I'm going to set the width of the UL, I'm going to create a variable and name it imageBox because our ul tag has all the images. I'm going to write like such has shown below var imageBox = $('.slider ul');
We can also find out the quantity images by writing var imageQuantity = $('.slider ul').children('li').length;

$(document).ready(function($){
var imageBox = $('.slider ul');
var imageWidth = $('.slider ul li').first().children('img').width(); var imageQuantity = $('.slider ul').children('li').length; imageBox.css('width', imageWidth*) console.log(imageWidth);
});


In the console if I type $('.slider ul') it will return the ul tag as shown below. if I write $('.slider ul').children('li') It will return the properties of li's. If I write $('.slider ul').children('li').length it will return the number of li's it has. Since we have three images in our slider therefore it's going to return 3.



Now that we know the image quantity so I'll change it to 3

$(document).ready(function($){
var imageBox = $('.slider ul');
var imageWidth = $('.slider ul li').first().children('img').width();
var imageQuantity = $('.slider ul').children('li').length;

imageBox.css('width', imageWidth*imageQuantity);
console.log(imageWidth);
});
Also I'm going to change the width of slider ul to 500px and set the left value to 0 like such


Save it, refresh the screen and you see the first image is showing and


Now when the button is clicked, the sliding effect should happen. When the button is clicked, jQuery is going to figure out which button is clicked and then run the function, based on whatever button was clicked. I'm going to say if next button is clicked (whichButton === 'next') run certain function, if previous button is clicked else if(whichButton === 'prev') then do something else like such
 


Save it, refresh the screen and now if you click on the next button, the next image will show up, if you hit next again, nothing will happen because the button is static. And the left button goes to the +500 px which is incorrect in our scenario



I want to explain something to you. Learning a programming language is not a very difficult task. People struggle with how to use tools of JavaScript or jQuery and break it down into logical steps so that you can use these tools into doing something fancy like making a photo slider.  

Concept 
The first image should have a left position of 0 px
The second image should have a left position of -500px
The third image should have a left position of -1000px
  -0px
  -500px
  -1000px

The 500 is the px width of one image so we can come up with a mathematical formula
2 * width = -500 (2 times a width of 500px is really a 1000px, but we want to get to the 500px so we can say
(2-1) * width = -500 (2-1 = 1 times to 500px = 500
Same we can say
(1-1) * width = -0 -----> (1 image minus 1 image = 0)
(2-1) * width = -500 -----> (2-1 = 1 times to 500px = 500
(3-1) * width = -1000px -----> (3 image minus 1 image = 2 and 2 times the width of the image 500px is = 1000px

We converted that logic into a mathematical formula, and now we can give this formula to jQuery or JavaScript and its going to consistently calculate the pix from left to right no matter we have 1 image or thousand images. In the above formula -1 is basically in the formula now we want to figure out if our current image is number 1, number 2 or number 3.
I'm going to create a variable and name it currentImage and the default image is going to be 1. Also I'm going to remove multiple vars from our code and separate each line with a comma and the last statement with a semi colon like such



So when the next button is clicked, the function is going to run and the next image will load, make sure the code looks similar to the screenshot below


Save it, refresh the screen and now if you click the next button, the second image will show up. If you click the next button again, the third image will show up. But if you click the next button, the fourth image would have appeared but since we don't have any fourth image, it will look like similar to the screenshot below


We can limit the slider so it doesn't go on and on and on. The same thing happens in the previous button, it shows the 3rd image, the 2nd image and the first image but when the previous button is clicked again, it leads to the similar whitish thing. 
We need to limit our slider so when the last image is being viewed and the next button is clicked, it leads all the way to the first image.
Optimizing your code
You may notice in the image below, that there is a duplication
 

We can arrange the code by putting it in the function and call it transition because it's actually transitioning the ul tag. 


Another good optimization to do when writing code is to structure the code in such a way that your functions are not accessing any global values but wherever the function is called, it passes the values that the function has to use. in the image below pxValue has two variables "currentImage" and the "imageWidth" I can pass these values inside the transition as shown below


The function is now isolated and can be copied and pasted in any JavaScript program and can be used. The code is reusable and better when you make bigger projects. 

One last thing I'm going to do is that we don't want to go to the blank page when the last image is clicked, I want to go to the first image when the last image is being viewed and clicked for the next one.

So I can say if the next button is clicked and the value of current image is imageQuantity (which is 3 in our case since we have 3 images) so in this case we want to set the currentImage value to 1 so the image goes to the first image

I'm also going to call the transition function as shown below but if it is not on the last image then we are going to use our old logic of incrementing the value and go to the next image


For the previous button, If the currentImage is on the first image and you hit the previous button then you want to go to the last image. Make sure you change the values as shown in the screenshot below.


Now if I save it, refresh the page and click buttons in the slider, it will work smoothly. If you're on the last image and hit next, it will take you to the first image and if you're on the first image and hit previous it will take you to the last image.

Grab the source code below and play around with the code and build your own slider. I hope you enjoyed this lesson. If you have any question, leave your comments below. I'll talk to you in the next lesson. Good bye :)

Become a confident software developer with mentor support!
Get Started
Already have an account? Please Login