Enroll in the training to get access to this lesson along with hundreds of other lessons and complete courses.
Already enrolled? Login
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 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 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 0 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);
});
var imageBox = $('.slider ul');
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);
});
$('.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.$(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 suchSave it, refresh the screen and you see the first image is showing and
(whichButton === 'next')
run certain function, if previous button is clicked else if(whichButton === 'prev')
then do something else like suchSave 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
The second image should have a left position of -500px
The third image should have a left position of -1000px
-500px
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
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.
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 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 :)
Get Started
Already have an account? Please Login