Maintain the structure of the slick slider during transitions

I am working with the slick slider and aiming to achieve a specific layout for the slider itself. What I require is a structure that looks like this:

div             div
    div     div
        div

I have managed to implement this design successfully, but when it transitions or slides automatically, it reverts to a different layout:

div div div div div

It returns to the original design once the transition is complete. I am curious to know if it is possible to maintain the initial layout during the transition. Below you'll find the CSS, HTML, and jQuery code I am currently using.

HTML:

<div class="loop">
  <div class="product"> Your Content </div>
  <div class="product"> Your Content </div>
  <div class="product"> Your Content </div>
  <div class="product"> Your Content </div>
  <div class="product"> Your Content </div>
  <div class="product"> Your Content </div>
  <div class="product"> Your Content </div>
</div>

CSS:

.active:first-child {
    margin-top: 10px; }

.test, .slick-active:nth-child(1), .slick-active:nth-child(5) {
    margin-top: 10px; }

.test-2, .slick-active:nth-child(2), .slick-active:nth-child(4) {
    margin-top: 40px; }

.center-test, .slick-active:nth-child(3) {
    margin-top: 70px; }

JavaScript:

$(document).ready(function () {
var loop = $(".loop");
loop.slick({
    slidesToShow: 5,
    sliderPerRow: 1,
    swipeToSlide: true
});
loop.on('afterChange', function (event, slick, currentSlide, nextSlide) {
    loop.find(".slick-active").first().addClass("test");
    loop.find(".slick-active").last().addClass("test");
    loop.find(".slick-active").eq(1).addClass("test-2");
    loop.find(".slick-active").eq(3).addClass("test-2");
    loop.find(".slick-active").eq(2).addClass("center-test");

});
loop.on('beforeChange', function (event, slick, currentSlide, nextSlide) {
    loop.find(".slick-active ").removeClass("test");
    loop.find(".slick-active ").removeClass("test-2");
    loop.find(".slick-active ").removeClass("center-test");

});
loop.find(".slick-active").removeClass("test");
loop.find(".slick-active").first().addClass("test");
loop.find(".slick-active").last().addClass("test");
loop.find(".slick-active").eq(1).addClass("test-2");
loop.find(".slick-active").eq(3).addClass("test-2");
loop.find(".slick-active").eq(2).addClass("center-test");
});

To achieve the desired layout during the transition, it might be necessary to use an offset instead of a margin. Refer to the slick slider documentation for more information:

EDIT

I have also included additional CSS code instead of applying margins to the div elements.

.test, .slick-active:nth-child(1), .slick-active:nth-child(5){
top:10px;
position:relative;
}
.test-2, .slick-active:nth-child(2), .slick-active:nth-child(4){
top:40px;
position:relative;
}
.center-test, .slick-active:nth-child(3){
top:70px;
position:relative;
}
.slick-track{
height:100px;
}

After observing the behavior, it seems that the class for the structural change is applied after the slide transition is completed, as indicated by 'afterChange' in the JS. Is it possible to apply the structure during the transition itself?

Answer №1

An issue that is easily recognizable here is the problem of infinite looping.
Perhaps one day, someone will be able to resolve this issue, but for now, if you are willing to compromise on that, this solution should work well.
This solution is adaptable to any number of slides.

$(document).ready(function () {
  var $loop = $(".loop");
  
  $loop.slick({
      slidesToShow: 5,
      sliderPerRow: 1,
      swipeToSlide: true,
      speed: 500,
      infinite: false
  });

});
.slick-track{
  height:100px;
}


.loop .product {
  background: #ccc;
  outline: 1px solid black;
  transition: transform .5s; /* same duration as in js */
  transform: translateY(0);
}


.loop .product.slick-current + .product {
  transform: translateY(30px);
}

.loop .product.slick-current + .product + .product {
  transform: translateY(50px);
}

.loop .product.slick-current + .product + .product + .product {
  transform: translateY(30px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/jquery.slick/1.5.7/slick.css"/>
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery.slick/1.5.7/slick.min.js"></script>


<div class="loop">
  <div class="product"> Your ContentA </div>
  <div class="product"> Your ContentB </div>
  <div class="product"> Your ContentC </div>
  <div class="product"> Your Content1 </div>
  <div class="product"> Your Content2 </div>
  <div class="product"> Your Content3 </div>
  <div class="product"> Your Content4 </div>
  <div class="product"> Your Content5 </div>
  <div class="product"> Your Content6 </div>
  <div class="product"> Your Content7 </div>
</div>

Answer №2

It seems that when using slick slider, elements are added and removed as you slide or swipe, which may cause issues with the nth-child selector you have used.

Instead of the nth selector, try using class names like this:

.test, .slick-active:frstchild {
    top: 10px;
    position: relative;
}

<div class="loop">
    <div class="product frstchild"> Your Content </div>
    <div class="product"> Your Content </div>
    <div class="product"> Your Content </div>
    <div class="product"> Your Content </div>
    <div class="product frstchild"> Your Content </div>
    <div class="product"> Your Content </div>
    <div class="product"> Your Content </div>
</div>

Alternatively, you can adjust the nth-child selector to match the cycle size like this:

.test, .slick-active:frstchild(3n+1){}

Answer №3

Give this a try:

.product:nth-child(5n+1),
.product:nth-child(5n+5)
{
    top:10px;
    position:relative;
}
.product:nth-child(5n+2),
.product:nth-child(5n+4)
{
    top:40px;
    position:relative;
}
.product:nth-child(5n+3)
{
    top:70px;
    position:relative;
}

When using Slick slider, additional elements are added (like slick-cloned), which can affect styling. By utilizing nth-child, you can solve styling issues without resorting to complex JavaScript solutions ;) Here is a helpful explanation of how nth-child works.

However, be cautious when utilizing nth-child as it may not always behave as expected. For instance:

<style>
    .product:nth-child(1){background:red;}
    .product:nth-child(2){background:blue;}
</style>
<div class="loop">
    <div class="promo"> Some promo </div>
    <div class="product"> Your Content1 </div>
    <div class="product"> Your Content2 </div>
</div>

Can you predict the backgrounds of these elements?

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

Automatic line breaks within a JavaScript code can be achieved by using

I need help formatting this text: hello everyone. My name is PETER. hahahah ahahah .... If I have a fixed width, how can I automatically line break the text to look like this: hello everyone. My name is PETER. hahahah ahahah ...

The Django/Python function, toDataURL, outputs a response containing the string retrieved from the toDataURL method

I recently stored a string in a database that was returned by a javascript method called toDataURL. An example of the string can be found here: http://pastebin.com/0Qu8rngD My goal is to retrieve the image in a Django response. I've attempted various ...

Capturing radio change events in jQuery: the definitive guide

I'm struggling to capture a simple list of radio buttons. Despite trying various solutions from SO and YouTube, nothing seems to be working for me. Below is the snippet of HTML code: <ul id="shipping_method"> <li> ...

angularjs bootstrap carousel not functioning as expected

HTML How do I apply an active class to the first image? I am trying to create a slideshow with images from a database but encountering issues. <div id="myCarousel" class="carousel slide" data-ride="carousel"> <div ...

How can I dynamically assign a className in a React component based on the current state when importing styles?

I've been utilizing the style-loader to insert CSS modularly into my components ({style.exampleClassName}). My goal is to showcase a loader for a specific duration before displaying an image (at least 16 of these components in a grid layout). This i ...

The local authentication feature in NodeJS Passport is experiencing issues and not functioning properly

I have integrated passportjs local for authentication. However, I am facing an issue where it always redirects to the failureRedirect without displaying any error messages. The redirect also includes the original username and password, resulting in a dupli ...

After deploying to Firebase, animations suddenly cease to function

After running npm run-script build and deploying my React app to Firebase hosting with firebase deploy, I encountered an issue where my animations stopped working. I'm puzzled as to why this is happening, especially since I've included keyframes ...

Tips for evaluating an array of objects in JavaScript

Welcome to the world of coding! Here's a scenario with an array: [ { "question1": "Apple", "question2": 5, "question3": "Item 1" }, { "question1": ...

Using the position property of relative on a DIV element causes a gap to appear between the DIV and the next

Two div elements are placed consecutively. When I move the first div using position: relative and top: -60px, it creates a gap between them. Take a look at this example here: https://codepen.io/dusannis/pen/oNgBpoK If you notice, there is a noticeable ga ...

Can the jQuery library be placed within the body of the webpage?

I have a simple question that I haven't been able to find an answer to yet. 1. Should the jquery library be placed in the body or the head of the HTML document? I'm specifically referring to the jquery library itself, not any additional jquery s ...

Is there a way to retrieve all CSS styles associated with a specific element?

When I come across a site element that I really like and want to incorporate into my own site, what is a simple way to do so? It can be challenging to navigate through numerous CSS files to find all the necessary styles. ...

What steps should be taken to validate a condition prior to launching a NextJS application?

In my NextJS project (version 13.2.4), I usually start the app by running: npm run dev But there's a new requirement where I need to check for a specific condition before starting the app. If this condition is not met, the app should exit. The condi ...

Troubleshooting: jQuery's clone( true ) function fails to function properly with dynamic

Consider this scenario: $.fn.foo = function() { var $input = $('<input type="text"/>'); var $button_one = $('<button>One</button>'); var $button_two = $('<button>Two</button>'); ...

Combine two elements together and display the outcome in json form

There are two objects that need to be summed up and returned in the same format as the original objects stored in a local file. The first JSON: { "data": [{ "x": "Q1 (J, F, M)", "y": [100, 500, 0], "tooltip": "this is tooltip" }, { "x": "Q2(A, M, ...

Tips for aligning three cards in a row using Bootstrap-5

In my Django template for loop, I am trying to display the first three cards from the database on the same line. Currently, only two of them are staying on the same line and I want all three cards to be aligned horizontally. <div class="row"&g ...

Is there a way to automatically submit an upload form once a file has been selected?

Need help with automatically submitting a file upload form when a file is selected. Is there a way to bypass the need for the user to click the Submit button? ...

Resolve a container overflow problem within the footer section

Trying to adjust the footer (newsletter signup form) on this page to fall to the bottom of the page has been a challenge. The #container element appears to be larger than the body, causing layout issues. Any suggestions on how to resolve this? An image d ...

Apply a class when a form field is deemed invalid

I am working on using jQuery validation for ajax form submission, and I am facing an issue where I would like to add a class to the errorContainer if any of the text inputs have a class 'error'. It seems simple, but I believe it may be a scope is ...

Having trouble getting a Chrome extension/jQuery to work on certain sites but not others? Attempting to trigger an event when a link is clicked

For three different websites, I have three separate js files. I want to mention that the manifest has the correct settings to make sure these files work on the right sites, but unfortunately, only one function of the extension (the most important one) is n ...

Having trouble loading a 3D model with ThreeJS in React?

I am currently utilizing React and ThreeJS to incorporate a 3D model into a webpage. However, I am encountering an issue with the function responsible for loading the mesh into my ThreeJS scene. The error message I am receiving is as follows: TypeError: C ...