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.

Initialize embla after certain width, and destroy below certain width

See original GitHub issue

Hello, i have a problem with initializing Embla carousel after 992px and destroying it below that value.

My js code:

const initBrandsEmbla = () => {
  const brandsEmbla = document.querySelector('.embla__brands');

  if (brandsEmbla !== null) {
    const settings = {
      draggable: false,
      loop: true,
      align: 'start',
      startIndex: 1,
    };

    const embla = EmblaCarousel(brandsEmbla, settings);
    const autoPlayer = autoPlay(embla, 1000);

    const checkEmbla = () => {
      if (window.matchMedia('(max-width: 992px)').matches) {
        // I want to reinitialize it here
        // something like embla.recover() would be helpful
        autoPlayer.play();
      } else {
        embla.destroy();
      }
    }

    checkEmbla();

    // On embla resize, check the width and initialize carousel, or destroy it
    embla.on('resize', () => {
      checkEmbla();
    });
  }
}

window.addEventListener('load', () => {
  initBrandsEmbla();
});

The problem is: it works when i load the window that has width lower than 992, it also works as intended (the carousel stops) when i resize that window above 992px, but when i go back below 992px, the carousel doesn’t recover or reinitialize no matter what i do. I tried many things, but nothing worked so far.

When i try something like that:

const initBrandsEmbla = () => {
  const brandsEmbla = document.querySelector('.embla__brands');

  if (brandsEmbla !== null) {
    const settings = {
      draggable: false,
      loop: true,
      align: 'start',
      startIndex: 1,
    };

    let embla;
    let autoPlayer;

    const checkEmbla = () => {
      if (window.matchMedia('(max-width: 992px)').matches) {
        // I want to reinitialize it here
        embla = EmblaCarousel(brandsEmbla, settings);
        autoPlayer = autoPlay(embla, 1000).play();
      } else {
        embla.destroy();
      }
    }

    checkEmbla();

    // On embla resize, check the width and initialize carousel, or destroy it
    window.addEventListener('resize', () => {
      checkEmbla();
    });
  }
}

window.addEventListener('load', () => {
  initBrandsEmbla();
});

Then it doesn’t stop on the desktop, i’ve ran out of ideas 😕

Do you know the solution by any chance?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
davidjerlekecommented, Nov 28, 2019

Hi Jakub (@jakubreron),

Thank you for the CodeSandbox! The reason why the carousel wasn’t destroyed is because autoPlayer wasn’t correctly stored in a variable and stopped before destroying the Embla instance. I’ve added the following and it now works as intended:

if (window.innerWidth < 992) {
  if (embla === null) {
    embla = EmblaCarousel(brandsEmbla, settings);
    autoPlayer = autoPlay(embla, 1000); // Store autoPlayer in a variable
    autoPlayer.play();
  }
} else {
  if (embla !== null) {
    autoPlayer.stop(); // stop autoPlayer before destroying Embla
    embla.destroy();
    embla = null;
    autoPlayer = null;
  }
}

👉 Here’s a working example. I hope this helps.

Kindly, David

1reaction
jakubreroncommented, Nov 28, 2019

Thank You so much! You are a hero! 💪 I will recommend Embla carousel to everyone in my company! 😀

Read more comments on GitHub >

github_iconTop Results From Across the Web

embla-carousel - npm
Embla Carousel. Extensible bare bone carousels for the web. Build awesome carousels by extending them with your own CSS and JavaScript.
Read more >
Methods | Embla Carousel
Methods. Embla Carousel exposes a set of useful methods which makes it very extensible. ... Hard reset the carousel after it has been...
Read more >
@wavenet/splide - npm Package Health Analysis | Snyk
Each slide can have its own width. Breakpoints accept 'destroy' option. Merge Slides component to Elements. Splide can be destroyed.
Read more >
How to turn on/off the embla carousel by breakpoint in React?
I'm guessing it's because destory was called so it can reInit and tried init , but no luck there either. const emblaOptions =...
Read more >
Draggable & Touch-friendly Carousel In Vanilla JavaScript
scrollPrevious() // goes to a specific slide embla. ... callback) embla.on('init', function(e){ // on init }) embla.on('destroy', function(e){ // on destroy }) ...
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