1

Tenho esse código de um carousel

$('#carousel-example-generic').on('slide.bs.carousel', function() {
  var interval = $('div.item.active').attr('duration');
  setTimeout(function() {
    $('.carousel').carousel('next');
  }, interval);
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>


<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
    <li data-target="#carousel-example-generic" data-slide-to="3"></li>
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active" duration="3000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        First
      </div>
    </div>
    <div class="item" duration="2000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Second
      </div>
    </div>
    <div class="item" duration="1000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Third
      </div>
    </div>
    <div class="item" duration="500">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Fourth
      </div>
    </div>
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

Como faço para repetir esse código e ter múltiplos carrossel na mesma página?

estou fazendo assim, mais não da certo

$('#carousel-example-generic').on('slide.bs.carousel', function() {
var interval = $('div.item.active').attr('duration');
setTimeout(function() {
$('.carousel').carousel('next');
}, interval);
});


$('#carousel-example-generic2').on('slide.bs.carousel', function() {
var interval = $('div.item.active').attr('duration');
setTimeout(function() {
$('.carousel2').carousel('next');
}, interval);
});
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  <!-- Indicators 
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
    <li data-target="#carousel-example-generic" data-slide-to="3"></li>
  </ol>-->

  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active" duration="4000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        First
      </div>
    </div>
    <div class="item" duration="8000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Second
      </div>
    </div>
    <div class="item" duration="4000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Third
      </div>
    </div>
    <div class="item" duration="8000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Fourth
      </div>
    </div>
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

















<div id="carousel-example-generic2" class="carousel slide" data-ride="carousel">
  <!-- Indicators 
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
    <li data-target="#carousel-example-generic" data-slide-to="3"></li>
  </ol>-->

  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active" duration="8000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        First
      </div>
    </div>
    <div class="item" duration="4000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Second
      </div>
    </div>
    <div class="item" duration="8000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Third
      </div>
    </div>
    <div class="item" duration="4000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Fourth
      </div>
    </div>
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic2" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#carousel-example-generic2" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

1

1 Resposta 1

0

Você quer passar cada item dos slides com tempos diferentes, pegando o atributo duration de cada.

A primeira coisa é que está usando o evento errado: use slid.bs.carousel em vez de slide.bs.carousel. O slid.bs.carousel é acionado após cada slide e assim você pode pegar corretamente o valor do atributo do item atual.

Está também chamando o método next com o seletor errado; Em vez da classe .carousel, use o id do slider (#carousel-example-generic e #carousel-example-generic2).

Retire também o atributo data-ride="carousel" para que o carousel inicie parado.

Veja o código corrigido e funcionando:

$(document).ready(function(){
   $('#carousel-example-generic').on('slid.bs.carousel', function(){
      var interval = $('div.item.active', this).attr('duration');
      var $this = $(this);
      setTimeout(function(){
         $this.carousel('next');
      }, interval);
   });
   
   $('#carousel-example-generic2').on('slid.bs.carousel', function(){
      var interval = $('div.item.active', this).attr('duration');
      var $this = $(this);
      setTimeout(function(){
         $this.carousel('next');
      }, interval);
   });

   setTimeout(function(){
      $('#carousel-example-generic').carousel('next');
   }, 1000); // coloque aqui o tempo do primeiro item do primeiro slider
   
   setTimeout(function(){
      $('#carousel-example-generic2').carousel('next');
   }, 3000); // coloque aqui o tempo do primeiro item do segundo slider
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<div id="carousel-example-generic" class="carousel slide">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
    <li data-target="#carousel-example-generic" data-slide-to="3"></li>
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active" duration="1000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        First
      </div>
    </div>
    <div class="item" duration="5000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Second
      </div>
    </div>
    <div class="item" duration="500">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Third
      </div>
    </div>
    <div class="item" duration="10000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Fourth
      </div>
    </div>
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

<!-- segundo slider -->

<div id="carousel-example-generic2" class="carousel slide">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
    <li data-target="#carousel-example-generic" data-slide-to="3"></li>
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active" duration="3000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        First
      </div>
    </div>
    <div class="item" duration="1000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Second
      </div>
    </div>
    <div class="item" duration="2000">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Third
      </div>
    </div>
    <div class="item" duration="500">
      <img src="http://placehold.it/400x200" alt="..." />
      <div class="carousel-caption">
        Fourth
      </div>
    </div>
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

Obs.: ao clicar nas setas "prev" e "next", será gerado um novo setTimeout que irá atropelar o que foi definido antes.

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .