Replace current items using Owl Carousel 2

血红的双手。 提交于 2020-03-03 06:19:05

问题


How is it possible to replace/rebuild owl carousel items with a new one?

Description

In many cases, like when you are getting new data from a database you would to add new data in your carousel, but in this example I try to replace all items with a new one.

$('.owl-carousel').owlCarousel({
    loop:true,
    margin:10,
    nav:true
});
<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>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/assets/owl.carousel.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/assets/owl.theme.default.min.css" />

<div class="owl-carousel owl-theme">
    <div class="item">
      <h4>Old 1-1</h4></div>
    <div class="item">
      <h4>Old 2-1</h4></div>
    <div class="item">
      <h4>Old 3-1</h4></div>
    <div class="item">
      <h4>Old 4-1</h4></div>
  </div>

回答1:


According to the Owl Carousel 2 documentation, you can use the replace.owl.carousel event.

replace.owl.carousel

Type: triggerable Parameter: data

Removes current content and add a new one passed in the parameter.

Usage

First you need to use replace trigger then append new data with refresh trigger. Do not use destroy or re-initialize

Example 1 / Update/Replace

$('.owl-carousel').owlCarousel({
  loop: true,
  margin: 10,
  nav: true
});

$('#reb').click(function() {
  var html = '<div class="owl-item"><h4>1-2</h4></div><div class="item"><h4>2-2</h4></div><div class="item"><h4>3-2</h4></div><div class="item"><h4>4-2</h4></div><div class="item"><h4>2-2</h4></div>';
  $('.owl-carousel').trigger('replace.owl.carousel', html).trigger('refresh.owl.carousel');
});
.btn {
    display: inline-block;
    padding: 6px 12px;
    margin-bottom: 0;
    font-size: 14px;
    font-weight: 400;
    line-height: 1.42857143;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    background-image: none;
    border: 1px solid transparent;
    border-radius: 4px;
}

.btn-success {
    color: #fff;
    background-color: #5cb85c;
    border-color: #4cae4c;
}
<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>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/assets/owl.carousel.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/assets/owl.theme.default.min.css" />

<div class="owl-carousel owl-theme">
  <div class="item">
    <h4>Old 1-1</h4>
  </div>
  <div class="item">
    <h4>Old 2-1</h4>
  </div>
  <div class="item">
    <h4>Old 3-1</h4>
  </div>
  <div class="item">
    <h4>Old 4-1</h4>
  </div>
</div>

<a id="reb" class="btn btn-success">Rebuild</a>

Example 2 / Update/Add

$('.owl-carousel').owlCarousel({
  loop: true,
  margin: 10,
  nav: true
});

$('#reb').click(function() {
  var html = '<h4>New item</h4>';
  $('.owl-carousel').trigger('add.owl.carousel', html).trigger('refresh.owl.carousel');
});
.btn {
    display: inline-block;
    padding: 6px 12px;
    margin-bottom: 0;
    font-size: 14px;
    font-weight: 400;
    line-height: 1.42857143;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    background-image: none;
    border: 1px solid transparent;
    border-radius: 4px;
}

.btn-success {
    color: #fff;
    background-color: #5cb85c;
    border-color: #4cae4c;
}
<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>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/assets/owl.carousel.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.2.1/assets/owl.theme.default.min.css" />

<div class="owl-carousel owl-theme">
  <div class="item">
    <h4>Old 1-1</h4>
  </div>
  <div class="item">
    <h4>Old 2-1</h4>
  </div>
  <div class="item">
    <h4>Old 3-1</h4>
  </div>
  <div class="item">
    <h4>Old 4-1</h4>
  </div>
</div>

<a id="reb" class="btn btn-success">Add</a>


来源:https://stackoverflow.com/questions/48033248/replace-current-items-using-owl-carousel-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!