最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - OwlCarousel autoheight on data update - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Jul 26, 2018 at 22:28 user3451822 asked Jul 20, 2018 at 8:06 AtCliff_AtCliff_ 1731 silver badge9 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 8 +50

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');

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>

var $owl = $('.owl-carousel').owlCarousel({
    items: 1,
    autoHeight: true
});
$('.owl-item').html('That was a hoot.')
$owl.trigger('refresh.owl.carousel');

Try this:

var currentItem = $('.owl-item.active');
var html = '<p class="item">Hello world et plus encore</p>';
currentItem.html(html);
发布评论

评论列表(0)

  1. 暂无评论