Skip to content

Instantly share code, notes, and snippets.

@kirandash
Last active April 17, 2023 09:44
Show Gist options
  • Save kirandash/fa0f6e131b2cd425f02a4da099e42458 to your computer and use it in GitHub Desktop.
Save kirandash/fa0f6e131b2cd425f02a4da099e42458 to your computer and use it in GitHub Desktop.
Owl Carousel Destroy on desktop and Initialize on mobile
function postsCarousel() {
var checkWidth = $(window).width();
var owlPost = $("#latest-posts .posts-wrapper");
if (checkWidth > 767) {
if (typeof owlPost.data('owl.carousel') != 'undefined') {
owlPost.data('owl.carousel').destroy();
}
owlPost.removeClass('owl-carousel');
} else if (checkWidth < 768) {
owlPost.addClass('owl-carousel');
owlPost.owlCarousel({
items: 1,
slideSpeed: 500,
animateOut: 'fadeOut',
touchDrag: false,
mouseDrag: false,
autoplay: true,
autoplaySpeed: 8000,
autoplayTimeout: 8000,
dots: true,
loop: true
});
}
}
postsCarousel();
$(window).resize(postsCarousel);
@matt5346
Copy link

Thanks man! finally found something useful)

@kirandash
Copy link
Author

@matt5346. Glad it helped! Happy Coding!

@Elchedli
Copy link

holy finally that works thank you so much kirandhash!!!!! i love you.

@kirandash
Copy link
Author

holy finally that works thank you so much kirandhash!!!!! i love you.

You are welcome @shidono.

@laurianne29
Copy link

Works very well for me thank youuu! 💯

@kirandash
Copy link
Author

Works very well for me thank youuu! 💯

@laurianne29 Glad it helped! You are welcome!

@viciouskitten
Copy link

viciouskitten commented Apr 3, 2021

This is so great thank you! If anyone is using the owl-theme you will need to add the class ".owl-theme" also
owlPost.removeClass('owl-carousel owl-theme');
owlPost.addClass('owl-carousel owl-theme');

@andyiwest
Copy link

Works for me. I was experiencing some rather odd styling when the slider was being destroyed and reinitialised. For whatever reason it would fail to destroy itself after being reinitialised while at mobile size, despite having it in a window resize function. Very strange but this has solved that problem for me

@lightman1990
Copy link

lightman1990 commented Aug 12, 2022

Overcomplication

function initOwl() {
    if (typeof jQuery == 'function') {
        jQuery('.subset-carousel').each(function () {
            var el = jQuery(this);
            if (jQuery(window).width() <= 767) {
                jQuery(this).owlCarousel({
                    items: 2,
                    stagePadding: 27,
                    loop: true
                });
            } else {
                jQuery(el).owlCarousel('destroy');
            }
        })
    }
}

window.addEventListener('DOMContentLoaded', function () {
    initOwl();
});

window.addEventListener('resize', function () {
    initOwl();
});

Vanila js resolution is also similar and you can change the addEventListeners to jQuery code.. Also just add owl-carousel class on the top level div. This is also code for multiple carousels on same page.

@kirandash
Copy link
Author

This is so great thank you! If anyone is using the owl-theme you will need to add the class ".owl-theme" also owlPost.removeClass('owl-carousel owl-theme'); owlPost.addClass('owl-carousel owl-theme');

@viciouskitten Glad it helped! Thanks for sharing the tips on adding "owl-theme"

@kirandash
Copy link
Author

Works for me. I was experiencing some rather odd styling when the slider was being destroyed and reinitialised. For whatever reason it would fail to destroy itself after being reinitialised while at mobile size, despite having it in a window resize function. Very strange but this has solved that problem for me

@andyiwest Glad it helped!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment