Jump to content

Owl carousel issue inside bootstrap tab


thara

Recommended Posts

I'm trying to add owl carousel for each bootstrap tab. Carousel in the first tab is loading perfectly, but when user clicks on second tab, owl carousel items are not loading. That mean on second tab it displays empty space instead of displaying carousel items.

 

My HTML is something like this:

  <!-- Nav tabs -->
  <ul class="nav nav-tabs" role="tablist">
    <li role="presentation" class="active"><a href="#female" aria-controls="female" role="tab" data-toggle="tab">Female</a></li>
    <li role="presentation"><a href="#male" aria-controls="male" role="tab" data-toggle="tab">Male</a></li>
  </ul>

  <!-- Tab panes -->
  <div class="tab-content">
    <div role="tabpanel" class="tab-pane active" id="female">
      <div data-items="1" data-lg-items="7" data-margin="20" class="owl-carousel" data-autoplay='false'>
        <div>
          <a href="#">
            <div>
              <img src="images/profile/profile1.jpg" alt="" class="img-responsive center-block">
            </div>
          </a>
        </div>        

        <div>
          <a href="#">
            <div>
              <img src="images/profile/profile2.jpg" alt="" class="img-responsive center-block">
            </div>
          </a>
        </div>        

        <div>
          <a href="#">
            <div>
              <img src="images/profile/profile3.jpg" alt="" class="img-responsive center-block">
            </div>
          </a>
        </div>             
      </div>
    </div>

    <div role="tabpanel" class="tab-pane" id="male">
      <div data-items="1" data-lg-items="7" data-margin="20" class="owl-carousel" data-autoplay='false'>
        <div>
          <a href="#">
            <div>
              <img src="images/profile/profile1.jpg" alt="" class="img-responsive center-block">
            </div>
          </a>
        </div>        

        <div>
          <a href="#">
            <div>
              <img src="images/profile/profile2.jpg" alt="" class="img-responsive center-block">
            </div>
          </a>
        </div>        

        <div>
          <a href="#">
            <div>
              <img src="images/profile/profile3.jpg" alt="" class="img-responsive center-block">
            </div>
          </a>
        </div>             
      </div>
    </div>
  </div>

This is my JS looks like:

var plugins = {
      bootstrapTabs: $(".tabs"),
      owl: $(".owl-carousel")
    };

if (plugins.owl.length) {
  var i;
  for (i = 0; i < plugins.owl.length; i++) {
      var c = $(plugins.owl[i]),
          responsive = {};
      var aliaces = ["-", "-xs-", "-sm-", "-md-", "-lg-"],
          values = [0, 480, 768, 992, 1200],
          j, k;
      for (j = 0; j < values.length; j++) {
          responsive[values[j]] = {};
          for (k = j; k >= -1; k--) {
              if (!responsive[values[j]]["items"] && c.attr("data" + aliaces[k] + "items")) {
                  responsive[values[j]]["items"] = k < 0 ? 1 : parseInt(c.attr("data" + aliaces[k] + "items"));
              }
              if (!responsive[values[j]]["stagePadding"] && responsive[values[j]]["stagePadding"] !== 0 && c.attr("data" + aliaces[k] + "stage-padding")) {
                  responsive[values[j]]["stagePadding"] = k < 0 ? 0 : parseInt(c.attr("data" + aliaces[k] + "stage-padding"));
              }
              if (!responsive[values[j]]["margin"] && responsive[values[j]]["margin"] !== 0 && c.attr("data" + aliaces[k] + "margin")) {
                  responsive[values[j]]["margin"] = k < 0 ? 30 : parseInt(c.attr("data" + aliaces[k] + "margin"));
              }
          }
      }
      c.owlCarousel({
          autoplay: c.attr("data-autoplay") === "true",
          loop: c.attr("data-loop") !== "false",
          items: 1,
          dotsContainer: c.attr("data-pagination-class") || false,
          navContainer: c.attr("data-navigation-class") || false,
          mouseDrag: "false",
          nav: c.attr("data-nav") === "true",
          dots: c.attr("data-dots") === "true",
          dotsEach: c.attr("data-dots-each") ? parseInt(c.attr("data-dots-each")) : false,
          animateIn: c.attr('data-animation-in') ? c.attr('data-animation-in') : 'fadeIn',
          animateOut: c.attr('data-animation-out') ? c.attr('data-animation-out') : 'fadeOut',
          responsive: responsive,
          navText: []
      });
  }

    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
      e.target // newly activated tab
      e.relatedTarget // previous active tab
      $(".owl-carousel").trigger('refresh.owl-carousel');
    });
}

So can anybody tell me how to fix this issue?

Hope somebody may help me out. Thank you.

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.