获取图像项计数并更新猫头鹰轮播 2 中的中心选项

Get the image item count and update the center option in owl carousel 2

本文关键字:选项 猫头鹰 图像 更新 获取      更新时间:2023-09-26

我正在尝试实现猫头鹰轮播,使用回调更新其中心选项。

$('.owl-carousel').owlCarousel({
    center: callback
});
function callback() { 
    if ( $('.owl-item .item').length == 1 ){
        return true; 
    }
    return false;

猫头鹰项目长度正确。但它没有更新中心选项。有没有更好的正确做法。

Owlcarousel 中心选项只期望真或假,布尔回调将起作用,但删除event参数。

$('.owl-carousel').owlCarousel({
        loop: true,
        margin: 10,
        center: function() {
          if ($('.owl-item .item').length == 1) {
            return true;
          }
          return false;
        },
      });

这样做:

jQuery(document).ready(function($) {
  $('.loop2').owlCarousel({
    loop: true,
    margin: 10,
    center: function() {
      if ($('.owl-item .item').length == 1) {
        return true;
      }
      return false;
    },
  });
});
<link href="http://www.owlcarousel.owlgraphic.com/assets/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet" />
<link href="http://www.owlcarousel.owlgraphic.com/assets/owlcarousel/assets/owl.theme.default.min.css" rel="stylesheet" />
<link href="http://www.owlcarousel.owlgraphic.com/assets/css/docs.theme.min.css" rel="stylesheet" />
<script src="http://www.owlcarousel.owlgraphic.com/assets/vendors/jquery.min.js"></script>
<script src="http://www.owlcarousel.owlgraphic.com/assets/owlcarousel/owl.carousel.js"></script>
<div id="demos">
  <div class="loop2 owl-carousel">
    <div class="item">
      <h4>1</h4>
    </div>
  </div>
</div>