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.

Set active class at the beginning of the slide animation

See original GitHub issue

I’m trying to understand why the active class on slides (glide__slide--active) is only set after the transition ended and not already when it starts - or why there are no intermediate classes for the status of sliding.

Is there a way to configure this behavior right now?

I’m asking because this would make parallel css transitions while it’s sliding possible (for example scaling the active slide a little bit), but with the current approach it’s only executed after the slide animation is finished.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:7
  • Comments:6

github_iconTop GitHub Comments

9reactions
elementalhubcommented, Sep 3, 2019

I got this to work with a custom component. Inside a component you can access other core components such as the Html Component, which lets you access the current slide element and add classes to it. I added my own is-active, is-next and is-prev classes to the respective elements and then added custom transitions via CSS.

import { siblings } from '@glidejs/glide/src/utils/dom';

...
const CustomActiveClass = function (Glide, Components, Events) {
    var Component = {
        mount() {
            this.changeActiveSlide();
        },

        changeActiveSlide() {
            let slide = Components.Html.slides[Glide.index];
            slide.classList.remove('is-next', 'is-prev');
            slide.classList.add('is-active');
 
            siblings(slide).forEach((sibling) => {
                sibling.classList.remove('is-active', 'is-next', 'is-prev');
            });

            if(slide.nextElementSibling) {
                slide.nextElementSibling.classList.add('is-next');
            }

            if(slide.previousElementSibling) {
                slide.previousElementSibling.classList.add('is-prev');
            }
        },
    };

    Events.on('run', () => {
        Component.changeActiveSlide();
    });

    return Component;
};

And then register the component when mounting:

glide.mount({
    'CustomActiveClass': CustomActiveClass,
});
1reaction
smitbarmasecommented, Oct 14, 2022

It partially works on the carousel type (Infinite scrolling). It breaks when transitioning from the last slide to the first one. I fixed it using by grabbing the inactive slide that is at the right of the last active slide, and the same for the first active slide by grabbing the previous inactive slide.

    slider.mount({
      CustomActiveClass: (Glide, Components, Events) => {
        var Component = {
          mount() {
            this.changeActiveSlide();
          },
          changeActiveSlide() {
            const slide = Components.Html.slides[Glide.index];

            // Add active animation to next index
            slide.classList.add("glide_slide-is-active");

            // Remove from all existing indices
            siblings(slide).forEach((sibling) => {
              sibling.classList.remove("glide_slide-is-active");
            });

            const lastIndex = Components.Html.slides.length - 1;

            // Fix for infinite scroll when next card
            if (Glide.index == 0) {
              Components.Html.slides[
                lastIndex
              ].nextElementSibling.classList.add(
                "glide_slide-is-active"
              );
            }

            // Fix for infinite scroll when prev card
            if (Glide.index == lastIndex) {
              Components.Html.slides[0].previousElementSibling.classList.add(
                "glide_slide-is-active"
              );
            }
          },
        };
        Events.on("run", () => {
          Component.changeActiveSlide();
        });
        return Component;
      },
    });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Slick Carousel target active slide to add and remove ...
slick-active class is dynamically generated by the slick carousel . I am targeting the p element in the above HTML code. The animated...
Read more >
Set active class at the beginning of the slide animation
I'm trying to understand why the active class on slides ( glide__slide--active ) is only set after the transition ended and not already...
Read more >
How To Add Active Class To Current Element - W3Schools
Learn how to add an active class to the current element with JavaScript. Highlight the active/current (pressed) button: 1 2 3 4 5....
Read more >
Using CSS animations - CSS: Cascading Style Sheets | MDN
Animations consist of two components, a style describing the CSS animation and a set of keyframes that indicate the start and end states...
Read more >
Carousel · Bootstrap v5.1
The .active class needs to be added to one of the slides otherwise the carousel will not be visible. Also be sure to...
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