How to Build a Simple jQuery Slider
In this article, we’re going to explore how you could set up a basic slider using a jQuery library. We’ll use the bxSlider library, which is built on top of jQuery and provides a lot of configuration options for setting up a slider.
Nowadays, an image slider is a must-have feature—a picture is worth a thousand words!
Once you’ve decided that you’re going to set up a basic slider, the next question is what exactly is the process of creating one. Of course, the first requirement is to collect good-quality, high-resolution images.
Next, you know that you’re going to use HTML and some fancy JavaScript stuff to create your image slider. And there are a lot of libraries available on the web that allow you to create a slider in different ways. We'll use the open-source bxSlider library.
The bxSlider library supports responsiveness, so a slider that's built using this library will adapt to any device. I’m sure you are aware how critical it is nowadays to build a website which is responsive and adaptive to different devices. Thus, responsiveness is a must-have feature when you choose a third-party library to build your sliders.
In the next section, we’ll start exploring the bxSlider library basics and setup process. Moving further, we’ll build a real-world example which will demonstrate the use of the bxSlider library. And finally, we’ll have a look at a few important parameters supported by the bxSlider library that allow you to customize a slider according to your requirements.
What Is bxSlider?
If you’re looking for jQuery-based content sliders, bxSlider is one of the best and simplest libraries, and it allows you to create content and image sliders very easily.
Let’s have a quick look at the features it provides:
- It’s fully responsive and adaptive to all types of devices.
- It supports different display modes like horizontal, vertical, and fade modes. We’ll see more on this later in the article.
- The slide content can be anything: image, video, or HTML text.
- It supports all popular browsers.
- It provides a variety of configuration options that allow you to customize a slider according to your custom requirements.
- Last but not least, it’s easy to implement, as we’ll see in the next section.
Now, let’s have a look at the installation process of the bxSlider library. In this article, I’m going to use CDN URLs to load the necessary JavaScript and CSS files, but of course you could always go ahead and download or clone these files from the official bxSlider GitHub repo.
Include JavaScript and CSS Files
The first thing that you need to do is to include the necessary JavaScript and CSS files in your HTML file, as shown in the following snippet.
1 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.css"> |
2 |
<script "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> |
3 |
<script "text/javascript" src="https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.min.js"></script> |
You can include the above code in the <head> tag of your HTML document. As you can see, I’ve loaded the necessary files from CDN URLs. If you’ve downloaded these files for your project, you need to provide the correct path for each file.
Set Up the Slider Content
In this section, we’ll see how you can set up the slider content in your HTML file.
Let’s have a look at the following snippet.
1 |
<div class="slider"> |
2 |
<h2>Slide One</h2> |
3 |
<h2>Slide Two</h2> |
4 |
<h2>Slide Three</h2> |
5 |
</div>
|
In the above example, we’ve set up three slides to rotate between when we initialize the slider. The important thing to note in the above snippet is the CSS class which is provided in the main <div> element. At the moment, we’ve used slider as our CSS class, so we'll use this value during the setup of bxSlider.
Of course, you could use any content, not just text. We’ll get back to this in the next section when we look at how to build a complete image slider. For now, you just need to note down the CSS class which we’ve provided in the main <div> element.
Our slider won't look very good with just raw HTML, so we will be adding some extra CSS that specifies the background, font-size, and text alignment of our headings.
1 |
body { |
2 |
margin: 20px auto; |
3 |
font-family: 'Lato'; |
4 |
font-weight: 300; |
5 |
width: 600px; |
6 |
}
|
7 |
|
8 |
div.slider h2 { |
9 |
text-align: center; |
10 |
background: orange; |
11 |
font-size: 6rem; |
12 |
line-height: 3; |
13 |
margin: 0; |
14 |
}
|
Initialize bxSlider
So far, we’ve included the necessary library files and set up HTML content for our slider. The only remaining thing is to initialize bxSlider to convert our static HTML content into a fancy-looking rotating slider!
Let’s have a look at the snippet to initialize our slider.
1 |
<script> |
2 |
$(document).ready(function(){ |
3 |
$('.slider').bxSlider(); |
4 |
});
|
5 |
</script> |
It’s JavaScript code, so you can also put it in the <head> tag. Or you can put it just above the </body> tag at the bottom of your HTML file, so that the JavaScript will run after the rest of your page is finished loading. If you prefer to put it in the <head> tag, you need to make sure to place it after you’ve loaded the necessary JavaScript and CSS files.
As you can see, we’ve used the slider CSS class to initialize our slider.
And with these three simple steps, you’ve built a responsive slider using the jQuery-based bxSlider library! Here is a CodePen demo that shows the slider in action: