Initialize embla after certain width, and destroy below certain width
See original GitHub issueHello, 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:
- Created 4 years ago
- Comments:11 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:👉 Here’s a working example. I hope this helps.
Kindly, David
Thank You so much! You are a hero! 💪 I will recommend Embla carousel to everyone in my company! 😀