12

I have 3 divs that activate slide toggle when I click on them. And inside every div there is owl carousel slider.

If I trigger one div the slider shows, but when I click other div slider doesn't show unless I resize the window.

How can I trigger refresh on slide toggle for the slider in every div?

I tried with this on slide toggle but it doesn't work:

$('.owl-slider').trigger('refresh.owl.carousel');
Gleb Kemarsky
  • 8,401
  • 6
  • 37
  • 57
vedran
  • 934
  • 1
  • 11
  • 18

3 Answers3

16

You trigger with a class. You can try with a variable:

var $owl = $('.owl-carousel').owlCarousel({
    items: 1,
    loop:true
});

$owl.trigger('refresh.owl.carousel');
Gleb Kemarsky
  • 8,401
  • 6
  • 37
  • 57
egvrcn
  • 904
  • 9
  • 24
9

if .trigger('refresh.owl.carousel'); didn't work with you, you can use

window.dispatchEvent(new Event('resize'));

which will make the carousel refresh automatically.

Moaaid Jamal
  • 101
  • 1
  • 5
  • Cool! I use Owl in React project so I had problem when after initialization my carousel with `items={`}` display with few items. I tried `$(window).trigger('resize');` but it's not worked. So I had played with this problem and soled it by added `onInitialized={window.dispatchEvent(new Event('resize'));}` – Стас Пишевский Apr 25 '20 at 19:56
1

I want to set new html content for my carousel and the above answers did not worked for me
so I solved my problem with another way

first, define a function to start owlCarousel and run that function

let myCarousel; //a variable thats hold owlCarousel object
function myCarouselStart() {
    myCarousel = $('#my-carousel.owl-carousel').owlCarousel(setting);
}

$(document).ready(() => {
    myCarouselStart(); // run owl carousel for first time
});

then when you want to refresh the carousel use the below code

 myCarousel.trigger("destroy.owl.carousel");
 $("#my-carousel").html(newHtmlContent);
 myCarouselStart();
Mahdi mehrabi
  • 668
  • 8
  • 16