简体   繁体   English

无法在Javascript中将动态项目添加到Owl Carousel

[英]Can not add dynamically items to Owl Carousel in Javascript

I'm trying to dynamically add items to an Owl carousel. 我正在尝试将项目动态添加到Owl轮播中。 Here is how I'm doing it: 这是我的做法:

HTML 的HTML

<div id="avatar-carousel" class="owl-carousel lesson-carousel">
                                            <div class="item item-logo">
                                              <div class="product-item">
                                                <div class="carousel-thumb" style="height: 77px!important;width: 70px;" >
                                                      <img src="http://placehold.it/140x100" alt="">
                                                </div>
                                              </div>
                                            </div>
                                              <!-- Start Item -->
                                        </div>

                                        <button id="click">
                                        click
                                        </button>

JS JS

$("#avatar-carousel").owlCarousel({

      items: 5
  });

  $("#click").click(function(){
    $('#avatar-carousel').trigger('add.owl.carousel', ['<div class="item"><p>' + 'hh' + '</p></div>'])
                    .trigger('refresh.owl.carousel');
  });

Nothing happens with this code. 此代码没有任何反应。 I can't see the error. 我看不到错误。 Here is the fiddle: JSFiddle . 这是小提琴: JSFiddle

EDIT: 编辑:

Seems like the code is correct, as it's working in the fiddle. 似乎代码是正确的,因为它正在摆弄。 I forgot to mention that the carousel works just fine, it is loaded correctly in the beginning. 我忘了提到轮播工作正常,它在一开始就已正确加载。 The issue is when trying to add items. 问题是尝试添加项目时。 Nothing happens, no errors but items are not added. 没有任何反应,没有错误,但未添加项目。

There can be two regular errors: 可能有两个常规错误:

  1. The button does not exist in time you create onclick event. 创建onclick事件时,该按钮不存在。
    • make sure the button exists in time you assign the event. 确保按钮在分配事件时及时存在。
  2. The carousel is inside the <form> and clicking the button submits the entire form (pay attention at browser page loading). 轮播位于<form> ,单击按钮将提交整个表单(请注意浏览器页面加载)。 Error shown here . 错误显示在这里

 $(document).ready(function() { $("#avatar-carousel").owlCarousel({ nav: true, items: 5 }); }); $("#click").click(function(e) { e.preventDefault(); //-- prevent form submit $('#avatar-carousel').trigger('add.owl.carousel', ['<div class="item"><img src="http://placehold.it/140x100" alt=""></div>']) .trigger('refresh.owl.carousel'); }); 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/assets/owl.theme.default.min.css" rel="stylesheet"/> <link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/assets/owl.carousel.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/owl.carousel.min.js"></script> <form action="" method="post"> <div id="avatar-carousel" class="owl-carousel"> <div class="item item-logo"> <img src="http://placehold.it/140x100" alt=""> </div> </div> <button id="click"> click </button> </form> 

It seems that all answers here work perfectly, but not in my case. 似乎这里的所有答案都可以正常工作,但就我而言并非如此。 Probably the events that are called are in conflict with others. 被调用的事件可能与其他事件发生冲突。 So I did it another way: 所以我用另一种方式做到了:

Clear the content of the carousel 清除轮播内容

$('#avatar-carousel').html('');

Append the new content into the carousel 将新内容附加到轮播中

imgs.forEach(function(img) {
  $('#avatar-carousel').append(createImgCarousel(img));
});

Where imgs is an array of image urls, and createImgCarousel a function that creates a carousel item. 其中imgs是图像网址的数组,而createImgCarousel是创建轮播项目的函数。

Finally reinitialize the carousel 最后重新初始化轮播

$("#avatar-carousel").owlCarousel({
  navigation: true,
  pagination: true,
  slideSpeed: 1000,
  stopOnHover: true,
  autoPlay: true,
  items: 5,
  itemsDesktopSmall: [1024, 3],
  itemsTablet: [600, 1],
  itemsMobile: [479, 1]
});

It's maybe not so clean, but it works for now ! 它可能不是很干净,但现在可以使用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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