Yogesh Chauhan's Blog

How to make a curtain slider using jQuery and CSS?

in CSS & jQuery on August 3, 2021

We can make use of jQuery and a little bit of CSS to make the curtain slider.

jQuery has mouse event handlers mouseover and mouseleave that can be used to trigger events when mouse is on top of an element and when mouse leaves the element.

What we can do is add a CSS class or just CSS styles on mouseover and mouseleave.

Adding a CSS class and handling the style in a seperate CSS file make the code more readable and doesn’t add much inline CSS.

Let’s try to implement that and see how far we can go.

Let’s add slides and background images for the slides.

Let’s also add the classes for hover state (mouse on) and mouse off state.

We can increase the width for the slide with mouse on and decrease the width for other slides. Also, for other slides, we can reduce the opacity to bring the focus on the slide with mouse on.

Since we have multiple slides, we need to add jQuery code for each slide. For that we can use forEach (each in jQuery) loop.


With the each loop we’re moving through each element with the class ‘.slide’. It’s basically like using getElementsByClassName in JavaScript.

We’re using this keyword to add a class to an element that has mouse hovered over it.

After that, to add ‘mouse-off’ class, we’re using ‘.slider‘ class selector and using jQuery children method, we’re getting all the children with a classname ‘slide‘. On top off that, we’re filtering out the children that don’t have ‘mouse-on’ class. Now, we’re left with the remaining children and we can add ‘mouse-off’ class to those elements.

The same way we have are removing class on mouseleave event.


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 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

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
How to change style of nth-child using jQuery?jQuerybin2hex() and chr() String Functions in PHPPHPHow to Use password_hash and password_verify to Secure Your User’s Data (Especially Passwords)?PHPThe simple difference between var, let and const in JavascriptJavaScriptHow to make a curtain slider using jQuery and CSS?CSSLearn to Establish Connection using MySQLi (object-oriented), MySQLi (procedural) and PDO with Example CodePHP