Home >Web Front-end >JS Tutorial >How to Build a Simple jQuery Slider
This article will guide you to create a simple picture carousel using the jQuery library. We will use the bxSlider library, which is built on jQuery and provides many configuration options to set up the carousel.
Now, picture carousel has become a must-have feature on the website - one picture is better than a thousand words!
After deciding to use the picture carousel, the next question is how to create it. First, you need to collect high-quality, high-resolution pictures.
Next, you need to create an image carousel using HTML and some JavaScript code. There are many libraries on the web that can help you create carousels in different ways. We will use the open source bxSlider library.
bxSlider library supports responsive design, so carousels built with this library can be adapted to any device. You sure know that it is crucial to build responsive websites and adapt to different devices today. Therefore, responsive design is an essential feature when selecting a third-party library to build a carousel.
In the next section, we will begin to explore the basics and setup of the bxSlider library. Next, we will build a practical example that demonstrates the use of the bxSlider library. Finally, we'll learn about some of the important parameters supported by the bxSlider library that allow you to customize the carousel to your needs.
If you are looking for jQuery-based content carousel, bxSlider is one of the best and easiest libraries, which allows you to create content and picture carousels very easily.
Let's take a quick look at what it offers:
Now, let's take a look at the installation process of the bxSlider library. In this article, I will use the CDN URL to load the necessary JavaScript and CSS files, but you can also download or clone these files from the official bxSlider GitHub repository.
The first thing you need to do is include the necessary JavaScript and CSS files in your HTML file, as shown in the code snippet below.
<code><link href="https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.css" rel="stylesheet"> <br><br><br><br></code>
You can include the above code in the tag of the HTML document. As you can see, I have loaded the necessary files from the CDN URL. If you have downloaded these files for your project, you need to provide the correct path for each file.
In this section, we will learn how to set carousel content in an HTML file.
Let's take a look at the following code snippet.
<code><div class="slider"> <br><h2>Slide One</h2> <br><h2>Slide Two</h2> <br><h2>Slide Three</h2> <br> </div> <br></code>
In the example above, we set up three slides that will be carouseled between them when the carousel is initialized. It should be noted that in the above code snippet, the CSS class is used in the main <div> element. Currently, we use <code>slider
as our CSS class, so we will use this value during the setup process of bxSlider.
Of course, you can use anything, not just text. We will return to this in the next section as we look at how to build a full picture carousel. Now you just need to note down the CSS class we provide in the main <div> element. <p>Our carousel doesn't look good using only raw HTML, so we'll add some extra CSS to specify the background, font size and text alignment of the title. </p>
<pre class="brush:php;toolbar:false">body {<br> margin: 20px auto;<br> font-family: 'Lato';<br> font-weight: 300;<br> width: 600px;<br>}<br><br>div.slider h2 {<br> text-align: center;<br> background: orange;<br> font-size: 6rem;<br> line-height: 3;<br> margin: 0;<br>}<br></pre>
<h3>Initialize bxSlider</h3>
<p>So far, we have included the necessary library files and set up HTML content for our carousel. The only thing left is to initialize bxSlider, converting our static HTML content into a beautifully-looking rotary carousel! </p>
<p>Let's look at the code snippet that initializes the carousel. </p>
<pre class="brush:php;toolbar:false"><br> $(document).ready(function(){<br> $('.slider').bxSlider();<br> });<br><br></pre>
<p>This is JavaScript code, so you can also put it in the <code><script></script>
tag. Or you can place it directly above the tag at the bottom of the HTML file to run JavaScript after the page is loaded. If you prefer to put it in the
<script></script>
tag, you need to make sure you place it after the necessary JavaScript and CSS files are loaded.
As you can see, we use the slider
CSS class to initialize our carousel.
Through these three simple steps, you build a responsive carousel using the jQuery-based bxSlider library! Here is a CodePen demonstration showing the actual effect of the carousel:
In the next section, we will expand to what we have discussed so far, and we will try to build the carousel by using the various configuration options provided by the bxSlider library.
In the previous section, we discussed how to set up a basic carousel using the bxSlider library. In this section, we will introduce a practical example that demonstrates how to build a rotating image carousel for your website.
In the root directory of your document, create an HTML file containing the following code snippets.
<br><br><br><br><link href="https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.css" rel="stylesheet"><br><br><br><br><div class="slider"> <br><div><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174162355471357.jpg" class="lazy" title="A Beautiful Landscape" alt="How to Build a Simple jQuery Slider" ></div> <br><div><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174162355685315.jpg" class="lazy" title="Stationery on Table" alt="How to Build a Simple jQuery Slider" ></div> <br><div><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174162355874714.jpg" class="lazy" title="A Coffee Mug" alt="How to Build a Simple jQuery Slider" ></div> <br><br><br> $('.slider').bxSlider({<br> autoControls: true,<br> auto: true,<br> pager: true,<br> slideWidth: 600,<br> mode: 'fade',<br> captions: true,<br> speed: 1000<br> });<br> <br><br><br><p>After setting, your slide should look like this: </p> <p><iframe allowfullscreen="true" frameborder="no" height="580" loading="lazy" scrolling="no" src="https://codepen.io/tutsplus/embed/LYredza?default-tab=result&editable=true&theme-id=light" width="850"></iframe></p> <p>The code in the above example should look familiar. It's very similar to what we've already discussed. The only difference is that we initialize our carousel with some configuration options supported by the bxSlider library. Let's take a closer look at this code snippet. </p> <pre class="brush:php;toolbar:false">$('.slider').bxSlider({<br> autoControls: true,<br> auto: true,<br> pager: true,<br> slideWidth: 600,<br> mode: 'fade',<br> captions: true,<br> speed: 1000<br>});<br>
autoControls parameter adds controls to allow users to start and stop slides. By default, it is set to false, so if you want to display the control you need to explicitly set it to true.
auto parameter allows you to automatically start the slide when the page is loading. By default, it is set to false.
pager parameter adds a pager to the slide.
slideWidth parameter allows you to set the width of the slide. This parameter is required if you use the horizontal option for your slideshow.
mode parameter allows you to configure the type of transition between slides. There are three options you can choose from - horizontal, vertical, and fade. In the example above, we used the fade-in option, so when you switch from one slide to another you will see the fade-in effect.
captions parameter is used if you want to display the title for each slide. The title is obtained from the title attribute of the image element. As you can see, we provide title attributes for all images in the example.
speed parameter allows you to configure the slide transition duration and set in milliseconds. In our example, we set it to 1000, so the slide will rotate once per second.
bxSlider has many other configuration options – you can learn about them in the official bxSlider options documentation. Continue to explore the different options available in the bxSlider library. It also provides many custom possibilities to use JavaScript callback methods.
Today, we discussed how to set up a basic carousel using the jQuery library. For demonstration, we chose the bxSlider library based on the jQuery library. We also built a practical example by using the various configuration options provided by the bxSlider library.
The above is the detailed content of How to Build a Simple jQuery Slider. For more information, please follow other related articles on the PHP Chinese website!