Enhance your slider experience: Applying a class name during the transition from left to right and right to left on S

How can I differentiate between left to right (ltr) and right to left (rtl) movements in a slick slider? I am looking to add unique class names for each direction movement.

Can anyone help me with this? Feel free to comment if you need more information.

This is the script I have attempted:

var dir;
if(dir=='right')
{
    $('.slick-active').addClass('right-effect');
}
if(dir=='left')
{
    $('.slick-active').addClass('left-effect');
}

console.clear();

$(".slider").slick({
    // autoplay: true,
    dots: true,
    arrows: false,
slidesToShow: 2,
slidesToScroll: 2
});
html, body {
 background: #102131;
 color: white;
}
 .container {
 padding: 30px;
}
 .test-case-wrap {
 margin-bottom: 60px;
}
 .slick-slider {
 background: #3a8999;
 font-size: 30px;
 line-height: 1;
 text-align: center;
 color: white;
}
 .slick-slider .slick-dots button:before, .slick-slider .slick-dots .slick-active button:before {
 color: rgba(255, 255, 255, .85);
}
 .slick-slider .slick-slide {
 background: #3a8999;
 padding: 40px 0;
}
 .slick-slider .slick-slide:nth-child(odd) {
 background: #e84a69;
}
 .slick-track {
 display: flex;
}
 .slick-track .slick-slide {
 display: flex;
 height: auto;
 align-items: center;
 justify-content: center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://rawgit.com/kenwheeler/slick/master/slick/slick.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://rawgit.com/kenwheeler/slick/master/slick/slick.js"></script>
<div class="container">
  <div class="slider">
    <div>Lorem ipsum dolor sit amet</div>
    <div>slide 1</div>
    <div>slide 2</div>
    <div>slide 3</div>
    <div>slide 4</div>
    <div>slide 5</div>
  </div>
</div>

Answer №1

Enhance your slider navigation by incorporating click() events:

$(".slider").slick({
    dots: false,
    slidesToShow: 2,
    slidesToScroll: 2
});
$(".slick-prev").on("click", function() {
   $('.slick-active').removeClass('right-effect');
   $('.slick-active').addClass('left-effect');
});
$(".slick-next").on("click", function() {
   $('.slick-active').removeClass('left-effect');
   $('.slick-active').addClass('right-effect');
});
html, body {
    background: #102131;
    color: white;
}
.container {
    padding: 30px;
}
.test-case-wrap {
    margin-bottom: 60px;
}
.slick-slider {
    background: #3a8999;
    font-size: 30px;
    line-height: 1;
    text-align: center;
    color: white;
}
.slick-slider .slick-dots button:before, .slick-slider .slick-dots .slick-active button:before {
    color: rgba(255, 255, 255, .85);
}
.slick-slider .slick-slide {
    background: #3a8999;
    padding: 40px 0;
}
.slick-slider .slick-slide:nth-child(odd) {
    background: #e84a69;
}
.slick-track {
    display: flex;
}
.slick-track .slick-slide {
    display: flex;
    height: auto;
    align-items: center;
    justify-content: center;
}
.right-effect {
    background-color:red !important;
}
.left-effect {
    background-color:blue !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://rawgit.com/kenwheeler/slick/master/slick/slick.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://rawgit.com/kenwheeler/slick/master/slick/slick.js"></script>
<div class="container">
  <div class="slider">
    <div>Lorem ipsum dolor sit amet</div>
    <div>slide 1</div>
    <div>slide 2</div>
    <div>slide 3</div>
    <div>slide 4</div>
    <div>slide 5</div>
  </div>
</div>

Additional Note: To optimize the dot navigation of the slider, use the following method:

 $(".slick-dots li").on("click", function() {
    let current = $(this).index();
    let previous = $(".slick-dots").attr("data-previous") ? $(".slick-dots").attr("data-previous") : 0;
    if (current > previous) {
       $('.slick-active').removeClass('left-effect');
       $('.slick-active').addClass('right-effect');
    } else if (current < previous) {
       $('.slick-active').removeClass('right-effect');
       $('.slick-active').addClass('left-effect');
    }
    $(".slick-dots").attr("data-previous", $(this).index());
 });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Certain Android devices may experience compatibility issues with the web app

I am facing an issue with my webapp on Raspberry Pi, as it is not being recognized properly by all my Android devices. The server hosting the website is a Raspberry Pi 3 running Apache and everything is up to date. To protect privacy and security, all IP ...

Having trouble with loading a 3D gltf model on A-frame.io?

I'm experiencing some difficulties when attempting to load a gltf model onto an a-frame. Unfortunately, the model does not appear to be loading properly. Here is the code snippet in question: <html> <head> <title>Tree model</t ...

Interactive dropdown menu with options for different actions

They say a picture is worth a thousand words. I have an idea for a feature I want to add to my Django web application: I want to create a dynamic dropdown list that allows users to add values to the database and dynamically updates itself with the new sel ...

Creating a Sum Column in a DataTables Editor

https://datatables.net/forums/discussion/45999 Is there a way to create a Total column which sums up three columns (No.1, No.2, No.3)? Here is an example table structure: ------------------------- No1 | No2 | No3 | Total | ------------------------- 4 ...

Populate an array using a callback function within an async.series operation

I have a callback function within 'async.series' that generates multiple values and creates various outputs from 'elements'. Is there a way to save these return values into an array using 'forEach'? async.series( { ...

Tips for eliminating default classes from Mui Typography styling

I’ve been working on creating a Typography element and noticed some default MUI CSS classes added when debugging in the browser. I attempted to disable them by using empty arrays at the end of the sx prop, but it did not work as expected. In the image p ...

Creating a flipbook out of a PDF file

I am looking for a cost-effective solution to convert my numerous PDF files into flipbooks for easy reading. I have been unable to find a free tool for this purpose, so I am curious if it is possible to create a flipbook using only HTML, CSS, and JavaScr ...

Exploring the contents of a JSON object

Just getting started with jquery and I'm attempting to parse a JSON object. This is my Jquery ajax call $(document).ready(function () { var resource = "v1/projects"; var url = '@Url.Action("Proxy")?resource=' + resource; ...

Checkbox acts like radio buttons in JavaScript

Currently, I have a unique setup where a table is generated dynamically based on the user's selection from a dropdown. Within this table are three checkboxes per row, with a limit of 2 checkboxes that can be checked per row. The behavior of Checkbox ...

Using HTML and JavaScript allows for the video URL to seamlessly open in the default video player app on the device

Working on my mediaplayer website, I want to give users the option to choose which app to play their uploaded videos with. So far, I've attempted to implement a button that triggers this action: window.open("video.mkv", '_blank'); Howeve ...

Is there a way to halt this interval once it reaches the end of the array?

I am attempting to create a simple animation using an array where it types out my name in a similar fashion to a command prompt. This is my first experience with React hooks, so I am feeling a bit lost. My goal is to have the interval stop at "Felipe Garci ...

Receiving alerts about props passed in MUI styled components triggering React's lack of recognition

I have a unique component design that requires dynamic props to determine its styling. Here is an example: const StyledTypography = styled(Typography)( ({ myColor = "black", isLarge = false }) => ({ "&&": { fontSi ...

Incorporate a generic type into a React Functional Component

I have developed the following component: import { FC } from "react"; export interface Option<T> { value: T; label: string; } interface TestComponentProps { name: string; options: Option<string>[]; value: string; onChang ...

Utilizing worldwide server session within an express node.js application

What is the best way to grant access to the current session for the modules in my node.js app when I can only access it within the request's function? My app is complex but modular, and passing the session between modules seems challenging as it wou ...

Issue with React: Reading the 'pathname' property from undefined is not possible

I can't figure out why this error keeps popping up. My goal is to create animated transitions between Routes. Can anyone point out where I went wrong? view image description here I tried reinstalling the react-spring package but it didn't solve t ...

After a postback, the Javascript function removes the displayed div

I am facing an issue with a page containing a button that uses JavaScript to display the content of a div. I also have an ASP.net validator control that triggers a postback. When I debug the JavaScript in Firebug, the div remains visible; however, if I all ...

What could be the reason for Angular to merge the element at index 0 of an array into a subarray instead of doing

After setting up the Array in my oninit function, I encountered an issue where one part of the array was functioning as intended while the other returned an error. this.tests = [{ status: 0, testresults: [{ name: 'test ...

Changing the class when a checkbox is selected using JQuery

I’m working on a bootstrap switcher and I want to change the panel color from grey to green when the checkbox (switch) is checked. I had it working before, but after updating the switcher, it no longer functions properly. Here is the main code for the s ...

How can I efficiently display three items per click when tapping into jQuery data?

Here is the code that I have been working on: jQuery(document).ready(function( $ ) { var $container = $('#homegrid').masonry({ isAnimated: true, itemSelector: '.home-blocks', columnWidth: '.grid-sizer ...

Is there a way to pass an object to the pointer configuration of a Gauge chart in HighCharts?

I'm having some difficulty setting up a highchart gauge chart. I want to pass an object with a name and value to the pointer, but it seems that it only accepts an array with a single number. Is there a way to include text along with the pointer's ...