15

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
  • 10,160
  • 7
  • 43
  • 68
vedran
  • 1,106
  • 2
  • 13
  • 18

3 Answers3

18

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
  • 10,160
  • 7
  • 43
  • 68
egvrcn
  • 974
  • 10
  • 27
13

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.

Muayad Jamal
  • 139
  • 1
  • 7
  • 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
  • Don't understand what these ginious are talking about. you should give any snippet, demo code,... – Shurvir Mori Apr 20 '22 at 17:10
2

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
  • 1,486
  • 2
  • 18
  • 21