Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OwlCarousel autoheight on data update

I have OwlCarousel, which has an autoheight parameter set to true. When I update the data inside the <div class = 'owl-item'> block, the height does not automatically change.\

I understand that this should not be, but, as an example, the block was empty, I added the data and it remained empty. Although, it's just a height of 1px.

Is it possible to change height on data inside changed?

like image 866
AtCliff_ Avatar asked Jul 20 '18 08:07

AtCliff_


2 Answers

Right after you've updated your <div class='owl-item'> you need to call the method to refresh your owl-carousel so it can update its height. Here an example.

But basically you can call it this way:

$('.owl-carousel').trigger('refresh.owl.carousel');
like image 58
Laura Avatar answered Sep 29 '22 13:09

Laura


Try this

$(document).ready(function() {
    const carousel = $(".owl-carousel")
    carousel.owlCarousel({
    items: 1,
    margin: 10,
    autoHeight: true
  });
   $(".item:first").html('update');
   carousel.trigger('refresh.owl.carousel');
});


$("#updateCarousel").on("click",function(e){
    $(".item:first").html('CONTENT<br><br>UPDATED');
    $(".owl-carousel").trigger('refresh.owl.carousel');
});
.owl-carousel .item {
  border: 2px solid red;
  background: red;
  width: 80%;
  margin:auto;
  color:white;

}
.owl-carousel{
  
    width:100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet"/>
<link href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.theme.default.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>
<div class="owl-carousel">
  <div class="item"> 1</div>
  <div class="item"> 2 </div>
  <div class="item"> 3 </div>
  <div class="item"> 4 </div>
  <div class="item"> 5 </div>
</div>
<button type="button" id="updateCarousel">Update Carousel</button>
like image 36
Fobos Avatar answered Sep 29 '22 13:09

Fobos