简体   繁体   中英

Owl carousel destroy issue (Uncaught TypeError: Cannot read property 'destroy' of undefined )

I am using Owl carousel for my project on certain resolution i need to destroy the owl carousel so i have used owl carousel destroy function but while using the function other jquery function is not working not even document ready Please help to fix this issue so that i can use the owl carousel destroy function along with jquery

here is my code

  function mobile() {

    var checkWidth = $(window).width();
    var banner = $("#ndmv-banner-intro");

      if(checkWidth >980){

        banner.owlCarousel({
        singleItem:true,
        autoPlay:false,
        dragBeforeAnimFinish : true
       });

        }else{
           banner.data('owlCarousel').destroy();
           banner.removeClass('owl-carousel').destroy();
        }
     }
     $(document).ready(mobile);
     $(window).resize(mobile);

demo url

You are applying the destroy function to an object which is undefined.You can try something like this.

if(typeof banner.data('owlCarousel') != 'undefined') {
    banner.data('owlCarousel').destroy();
    banner.removeClass('owl-carousel');
}

If this is your full code and you aren't just condensing it for easy reading, you are missing off the closing } for the mobile function.

It should be:

function mobile() {

  var checkWidth = $(window).width();
  var banner = $("#ndmv-banner-intro");

  if(checkWidth >980){

    banner.owlCarousel({
    singleItem:true,
    autoPlay:false,
    dragBeforeAnimFinish : true
   });

    }else{
       banner.data('owlCarousel').destroy();
       banner.removeClass('owl-carousel').destroy();
    }
}
$(document).ready(mobile);
$(window).resize(mobile);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM