question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Animation issues when going from last to first item.

See original GitHub issue

When I try to animate the slide transition with some opacity, everything works fine except when I go forward from last to first element. Even when I go backward from first to last it works fine.

.gif of the issue

The gif is a bit more glitchy than the actual result but we can clearly see that when going from slide “4” to “1” the animation skips. But not when going from slide “1” to “4”.

[ https://jsfiddle.net/b5bqk68j/6741/ ]

====================================================================

Steps to reproduce the problem

  1. Initialize a slick slider with the following config

    let sliderConfig = {
        centerMode: true,
        centerPadding: '400px',
        infinite: true,
        slidesToShow: 1,
        prevArrow: $prevArrow,
        nextArrow: $nextArrow
    };
    
  2. Add transition to slide or element in the slide. in my case:

    .slick-animate {
        opacity: 0.5;
        transition: opacity 0.48s ease-in-out;
    }
    .slick-center {
        .slick-animate {
           opacity: 1;
       }
    }
    
  3. Use any means of going forward through the slides and see the effect.

====================================================================

What is the expected behaviour?

Every slides animates properly (like when going backwards).

====================================================================

What is observed behaviour?

When going from last to first the animation is glitching. (probably because of the slide cloning/switching)

====================================================================

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
eleven59commented, May 12, 2021

Don’t know why this is closed since the issue is still here in the latest version. I fixed it in my project by applying a custom class “.slick-target” to the slide that slick is transitioning to (so next or prev slide depending on direction) and then applying CSS to both “.slick-target” and “.slick-current” simultaneously. No delay, just smooth transitions all around.

So JS:

// Init slider
let slider = $('.container').slick({'infinite': true});

// Add custom class to next or previous slide depending on change direction
slider.on('beforeChange', (event, slick, currentSlide, nextSlide) => {
    let nextIndex = currentSlide + 1; // assume moving right
    if(currentSlide-1 == nextSlide || (nextSlide+1 == slick.slideCount && currentSlide < nextSlide)) {
        nextIndex = currentSlide - 1; // nope, moving left
    }
    $(`[data-slick-index="${nextIndex}"]`).addClass('slick-target');
});

// clear custom class after transition
slider.on('afterChange', () => {
    $('.slick-slide').removeClass('slick-target');
});

And CSS:

/* Not current slide */
.slide {
    transform: scale(.8);
    transition: .5s all;
}

/* Current slide */
.slick-current .slide,
.slick-target .slide {
    transform: scale(1);
}

The detection in my JS code of whether we’re moving left or right only works for 3+ slides I think, because that was all I needed, but you could change that line to be much much longer if you need to detect this. See #1679.

2reactions
pjfsilvacommented, Nov 10, 2018

This should be reopened so it’s fixed in the library itself. maybe @explorador can send a PR for this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

html - Why are my animations rolling back to the initial list item ...
I'm having a problem where my two animations are both rolling back to the top after the last item in the animation is...
Read more >
CSS Animations Not Working? Try These Fixes - HubSpot Blog
How to Solve Common CSS Animation Issues. Whether your animation isn't working as intended, or just isn't working at all, here are some ......
Read more >
Trigger an animation effect - Microsoft Support
Trigger an animation effect to begin when you click it · Select the shape or object you want to add an animation to....
Read more >
animation-timing-function - CSS: Cascading Style Sheets | MDN
jump-start. Denotes a left-continuous function, so that the first jump happens when the animation begins;. jump-end.
Read more >
Create frame animations in Photoshop - Adobe Support
Last updated on Nov 15, 2022 | Also Applies to Adobe Photoshop CS6 More ... This is a quick way to make an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found