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.
background-image examples images mouseleave mouseover sass slider style