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 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to CREATE TABLE in SQL with and without using Another Table?SQL/MySQLIs monitoring an employee’s work on a computer a desirable or undesirable activity?MiscellaneousHow to make flexbox items of the same size?CSSWhat are Conditional Tags in WordPress?WordPressIs there a CSS parent selector?CSSThe fundamental difference between HAVING and WHERE clauses in PostgresPostgres