How to re-render a owl-carousel item?
Asked Answered
P

7

11

Well, I'm using a owl-carousel-2 plugin now.

And I encounter the following problem:

The markup code:

<div class="owl-carousel" style="display: none;">
    <div class="item"><img src="..." /></div>
    <div class="item"><img src="..." /></div>
    <!-- ... -->
    <div class="item"><img src="..." /></div>
</div>

<script>
$(function() {
    var $owl = $('.owl-carousel');
    $owl.owlCarousel();

    // Doing many things here.

    $owl.show();
});
</script>

The problem is:

When I initialize with the $owl.owlCarousel(); statement, which under an hidden state, its size is not initialized.

So when I show that control, the control displays in a mess!

But when I resize the window, it seemed to be triggering a re-render. The control render the contents, then displayed well.


So I'm wondering if there is a way to trigger this re-render (or refresh) method on it.

In order to make sure the control won't display in a mess.

I tried to read the documents and sources, but not yet have a good solution.

Please help.

Planter answered 14/1, 2015 at 16:29 Comment(2)
by "hidden state" do you mean display:none;? what happens if you use visibility:hidden; ?Detached
@Detached Yes, by display: none. But what I really want is the way of triggering a rerender.Planter
P
43

I found out an ugly, dirty solution. Anyway, it worked:

Main procedure:

  1. Destroy that owl-carousel.
  2. Manually change the markup to the initial state.
  3. Initialize the owl-carousel.

var $owl = $('.owl-carousel');
$owl.trigger('destroy.owl.carousel');
// After destory, the markup is still not the same with the initial.
// The differences are:
//   1. The initial content was wrapped by a 'div.owl-stage-outer';
//   2. The '.owl-carousel' itself has an '.owl-loaded' class attached;
//   We have to remove that before the new initialization.
$owl.html($owl.find('.owl-stage-outer').html()).removeClass('owl-loaded');
$owl.owlCarousel({
    // your initial option here, again.
});

It worked, but in such a dirty way. Hoping to see a better, neat solution.

Planter answered 16/1, 2015 at 2:52 Comment(2)
This is so far the only working solution I found across Internet.Bridgeport
$owl.html('') after destroying and then initialize and it worked.Farrahfarrand
N
15

This works for me:

// get owl element
var owl = $('.owl-carousel');

// get owl instance from element
var owlInstance = owl.data('owlCarousel');

// if instance is existing
if(owlInstance != null)
    owlInstance.reinit();

More information: http://owlgraphic.com/owlcarousel/demos/manipulations.html

Nerve answered 27/1, 2016 at 8:39 Comment(3)
This is the solution. My problem was when going fullscreen. The html would not re-render on Chrome only: as a result the content was blurry/pixelated.Lemley
Note that the above will work only for owlCarousel v1 (The OP is using v2).Calli
the link is brokenCribbage
A
6

Ran into the same issue with OP. I manage to get it to work by first removing the owl-loaded class. Then on render, trigger a refresh event after re-adding the class.

// Remove the owl-loaded class after initialisation 
$owl.owlCarousel().removeClass('owl-loaded');

// Show the carousel and trigger refresh
$owl.show(function() {
  $(this).addClass('owl-loaded').trigger('refresh.owl.carousel');
})
Anemography answered 9/7, 2015 at 5:1 Comment(0)
M
6

This is my solution, based on fish_ball's clever workaround:

if($('.owl-carousel').hasClass('owl-theme')){ //resize event was triggering an error, this if statement is to go around it

            $('.owl-carousel').trigger('destroy.owl.carousel'); //these 3 lines kill the owl, and returns the markup to the initial state
            $('.owl-carousel').find('.owl-stage-outer').children().unwrap();
            $('.owl-carousel').removeClass("owl-center owl-loaded owl-text-select-on");

            $(".owl-carousel").owlCarousel(); //re-initialise the owl
        }
Maxinemaxiskirt answered 16/6, 2016 at 14:40 Comment(0)
A
3

As for 2.0.0-beta.2.4 this works for me:

var $owl = $('.owl-carousel');
if ($owl.data('owlCarousel')) {
    $owl.data('owlCarousel').onThrottledResize();
}
Attaboy answered 20/9, 2016 at 8:6 Comment(0)
P
3

With Owl Carousel 2 you reinitialize like so:

jQuery('.owl-carousel').trigger('refresh.owl.carousel');

see this issue.

Pelvis answered 2/8, 2017 at 18:17 Comment(0)
D
2

Have you read the documentation of the plugin you are using, because the owl carousel has a refresh method when you want to update the carousel.

refresh.owl.carousel

Type: attachable, cancelable, triggerable
Callback: onRefresh
Parameter: [event, speed]

The docs are here for events

Detached answered 15/1, 2015 at 7:57 Comment(4)
how are you calling it?Detached
$owl.trigger('refresh.owl.carousel');Planter
refreshing doesn't seem to re-render. Tried and didn't work. Or maybe i'm missing something.Laurielaurier
the question here is from 7 years ago @Laurielaurier so prob no longer useful with current versionsDetached

© 2022 - 2024 — McMap. All rights reserved.