Last active
April 17, 2023 09:44
-
-
Save kirandash/fa0f6e131b2cd425f02a4da099e42458 to your computer and use it in GitHub Desktop.
Owl Carousel Destroy on desktop and Initialize on mobile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
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"
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
Overcomplication