javascript - Owl carousel's .destroy() removes unrelated divs - Stack Overflow

I want to switch between a carousel view and a different layout depending on the width of the viewport.

I want to switch between a carousel view and a different layout depending on the width of the viewport. Setting up the carousel works fine. I run into problems when I want to remove it.

I use $owlTeam.destroy(); which according to the documentation should recreate the state of the markup before the carousel was initialized, but for some reason removes two unexpected and crucial divs. One is a wrapper div that is a parent of the div containing the carousel the other is the carousel div itself.

This is my markup:

<section id="some-id" class="team">
  <div class="wrapper"> <!-- this gets removed on destroy -->
    <header><!-- content --></header>
    <div class="owlCarousel"> <!-- and this gets removed on destroy -->
      <article class="big"><!-- content contains another .wrapper --></article>
      <article class="big"><!-- content contains another .wrapper --></article>
      <article class="small"><!-- content --></article>
      <article class="small"><!-- content --></article>
      <!-- and some more articles -->
    </div>
  </div>
</section>

This is the JS I use:

var $owlTeam;
if( $window.width() < 680 ) {
    $('.team .owlCarousel').owlCarousel({
        autoPlay: false
        , singleItem:true
        , transitionStyle : "fade"
        , pagination : true
    });
    $owlTeam = $('.team .owlCarousel').data('owlCarousel');
}

$window.resize(function() {
    if( $window.width() < 680 ) {
        $('.team .owlCarousel').owlCarousel({
            autoPlay: false
            , singleItem:true
            , transitionStyle : "fade"
            , pagination : true
        });
        $owlTeam = $('.team .owlCarousel').data('owlCarousel');
    } else {
        if( typeof $owlTeam != 'undefined' ) {
            $owlTeam.destroy();
        }
    }
});

I tried using an ID to directly select the div that's supposed to contain the carousel but this didn't change the behaviour at all. I could reinsert the missing markup with JS but that seems to be more of a bandaid than a proper solution.

What is causing this behaviour and how can I fix it?

jQuery Version: 1.11.1 owl Version: 1.3.2 Browsers I tested: FF 35, Chrome 40

I want to switch between a carousel view and a different layout depending on the width of the viewport. Setting up the carousel works fine. I run into problems when I want to remove it.

I use $owlTeam.destroy(); which according to the documentation should recreate the state of the markup before the carousel was initialized, but for some reason removes two unexpected and crucial divs. One is a wrapper div that is a parent of the div containing the carousel the other is the carousel div itself.

This is my markup:

<section id="some-id" class="team">
  <div class="wrapper"> <!-- this gets removed on destroy -->
    <header><!-- content --></header>
    <div class="owlCarousel"> <!-- and this gets removed on destroy -->
      <article class="big"><!-- content contains another .wrapper --></article>
      <article class="big"><!-- content contains another .wrapper --></article>
      <article class="small"><!-- content --></article>
      <article class="small"><!-- content --></article>
      <!-- and some more articles -->
    </div>
  </div>
</section>

This is the JS I use:

var $owlTeam;
if( $window.width() < 680 ) {
    $('.team .owlCarousel').owlCarousel({
        autoPlay: false
        , singleItem:true
        , transitionStyle : "fade"
        , pagination : true
    });
    $owlTeam = $('.team .owlCarousel').data('owlCarousel');
}

$window.resize(function() {
    if( $window.width() < 680 ) {
        $('.team .owlCarousel').owlCarousel({
            autoPlay: false
            , singleItem:true
            , transitionStyle : "fade"
            , pagination : true
        });
        $owlTeam = $('.team .owlCarousel').data('owlCarousel');
    } else {
        if( typeof $owlTeam != 'undefined' ) {
            $owlTeam.destroy();
        }
    }
});

I tried using an ID to directly select the div that's supposed to contain the carousel but this didn't change the behaviour at all. I could reinsert the missing markup with JS but that seems to be more of a bandaid than a proper solution.

What is causing this behaviour and how can I fix it?

jQuery Version: 1.11.1 owl Version: 1.3.2 Browsers I tested: FF 35, Chrome 40

Share asked Feb 6, 2015 at 15:42 SeraphithanSeraphithan 4191 gold badge5 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

This is a reported bug: https://github./smashingboxes/OwlCarousel2/issues/460

Anyway you could try this to destroy the owl carousel:

$('#your_carousel').trigger('destroy.owl.carousel').removeClass('owl-carousel owl-loaded');
$('#your_carousel').find('.owl-stage-outer').children().unwrap();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745071261a4609561.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信