6

I have several owl carousels running. When it first loads the carousel flashes at full width until the jquery kicks in and then resizes everything. Is there anyway to stop this? Here's my code:

Html

 <?php $k='1'; do { ?>
 <div id="owlslide<?php echo $k;?>">
     <?php do { ?>
       <div class="owlItem ">
         <-- some other stuff -->
       </div>
     <?php  } while();?>
 </div>
 <?php $i++; } while();?>

Jquery (owl)

 $(document).ready(function(){
<?php $i='1'; do { ?>
 $("#owlslide<?php echo $i;?>").owlCarousel({
  autoPlay: false, //Set AutoPlay to 3 seconds
  paginationNumbers: true,
   itemsCustom : [
    [0, 1],
    [450, 1],
    [600, 2],
    [700, 2],
    [1000, 3],
    [1200, 4],
    [1400, 4],
    [1600, 5]
  ],        
  });
<?php $i++; }while($cara = mysql_fetch_assoc($catCara)); ?>

});
jcoffland
  • 5,238
  • 38
  • 43
Daniel Robinson
  • 643
  • 1
  • 10
  • 25

8 Answers8

11

You can hide the carousel items with display: none; in your CSS, then show them after the carousel has initialized by binding a handler to the initialized.owl.carousel event. I find it's best to combine it with a placeholder that has a loader gif to further reduce page jump.

var carousel = $('#owlslide');
carousel.on({

    'initialized.owl.carousel': function () {
         carousel.find('.item').show();
         carousel.find('.loading-placeholder').hide();
    }

}).owlCarousel(options);

Note that you have to bind the handler before you initialize the carousel.

Taytorious
  • 224
  • 2
  • 7
  • I couldn't get `initialized.owl.carousel` to work so I set style `.rotating-item { display:none }` and then after the owlCarousel jquery used the following to show `$('#rotator .rotating-item').show();` – Nick W Aug 20 '19 at 10:15
3

still not a best solution, but if you set the opacity to 0, it will work at some level and all the items will not be there on page load.

.owl-carousel:not(.owl-loaded){ 
    opacity: 0; 
}
1

If you are using OwlCarousel2, the plugin attaches the CSS class .owl-loaded to the .owl-carousel after it has finished rendering the carousel items. Simply set display: none on the container and reset display: block on the appended class.

i.e. In your CSS file:

.owl-carousel {
    display: none; 
}

.owl-carousel.owl-loaded {
    display: block;
}
jbiddick
  • 272
  • 2
  • 8
1
.owl-carousel:not(.owl-loaded){ 
    opacity: 0; 
    visibility:hidden;
    height:0;
}

Try this.

Shurvir Mori
  • 2,288
  • 1
  • 17
  • 29
1

Hello beloved developers,

I'm using Bootstrap 4 framework, so I added a .d-block class next to .owl-carousel class, and then I added a .d-none in all clild elements, except the 1rst one (I want the 1rst visible from the beginning).

So on init, you will need to run jQuery:

 var heroslider = $('#hero-slider');
    
    heroslider.on('initialize.owl.carousel', function(event) {
       $('#top-news').find(".owl-dot").eq(0).addClass("active");
       heroslider.find(".slider-item").removeClass('d-none');
    });

  heroslider.owlCarousel({
     loop: false,
     rewind: true,
     responsiveClass: true,
     margin: 0,
     dots: true,
     smartSpeed: 900,
     autoHeight: true,
     autoplay: true,
     autoplayTimeout: 8000,
     autoplayHoverPause: true,
     responsive:{
         0:{
             items:1,
             nav:false
         },
         600:{
              items:1,
              nav:false
         },
         1000:{
              items:1,
               nav:false
         }
      }
   });
Jokova
  • 166
  • 2
  • 5
1

Create a mask or an empty div to hide the complete slider block/div and add funtion to hide mask and show the slider on iniliatize event of owl carousel

    $('.my-carousel').owlCarousel({
        items: 1,
        lazyLoad: true,
        onInitialized: showSlider
    });

    // Call back function onInitialized
    function showSlider(e) {
        $(".my-carousel-mask").hide();
        $(".my-carousel-gallery").show();
    }
Arshad M
  • 452
  • 4
  • 14
0
$('.item').trigger('initialized.owl.carousel').show();
gvlasov
  • 18,638
  • 21
  • 74
  • 110
  • 2
    Welcome to Stackoverflow. Thank you for your answer to this question. To Improve this answer explain why this will solve the OP's problem and where in their example code they should put this. For more information about writing good answers, see https://stackoverflow.com/help/how-to-answer – Peter Campbell Apr 25 '16 at 14:36
0

Just hide images on loading and will auto show when loading complete!

.owl-carousel.loading{

 display:none;

}
Pan Bouradas
  • 179
  • 1
  • 6