Yogesh Chauhan's Blog

How to create a simple slider with CSS and jQuery?

in CSS & jQuery on March 7, 2021

We’re going to modify the following post example and convert the JavaScript code into a jQuery code.

Let’s start with simple HTML:

I have used div tags but you can always swap it with list tags. You can style the listanimate the list or few other things with the list items.

Let’s set the box area to border-box and set the main container position to relative and give it a fixed height.

We need to stack the slides on top of each other and we can do that by setting the position of slides absolute. We are NOT setting the opacity property and the transition property in this example as we did in the JS example. Also, add other styles as per your requirements.

We are keeping the image style same 100% and object-fit.

Everything was almost same as the JS example till this point.

Now, the main difference starts…

Let’s hide all children of div#slides and just show the first slide using nth-child.

…and finally, create the function to hide and show the slides one by one. To call the function periodically, we can use setInterval just like we used in JS example.

This is how it will look like afterwards:


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Introduction to components and templates Part 4: Pipes and DirectivesAngularSteps to Secure a VPNMiscellaneousHow to Block IPs and User Agents using code in Drupal or WordPress?Drupal4 different ways to create JavaScript ObjectsJavaScriptUseful (and probably ignored) HTML tags: Part 2HTMLHow to use images instead of HTML radio buttons using CSS?CSS