Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
Carousel not working if the code is added dynamically by jQuery

I've a carousel on my website, that works perfectly on hardcoding it. This is what the code looks like:

<div class="w-full flex justify-center">
   <div class="gallery js-flickity"
       data-flickity-options='{ "wrapAround": true }'
   >
     <div class="gallery-cell">
       <div class="w-full flex justify-center">
           <img src="./img/sidenav-1/clients-01.png" class="client-image"/>
       </div>
     </div>
     <div class="gallery-cell">
       <div class="w-full flex justify-center">
          <img src="./img/sidenav-1/clients-02.jpeg" class="client-image" />
       </div>
     </div>                  
 </div>

I've to use this carousel due to certain coding restricions I've so I can't change it. Now I have to connect this webpage to an admin panel, in which I'm using PHP for fetching the values from the database and echoing the same entire code and usind jQuery to put it in place. But If I do so instead of a carousel the images are displayed one beneath the other.

jQuery Code:

<script>
  $.ajax({url: "API/clients.php", success: function(result){
     $("#append_clients").html(result);
  }});
</script>

PHP:

$resultee = '<div class="gallery js-flickity" data-flickity-options=\'{ "wrapAround": true }\'>';

$sql = "SELECT * FROM Clients;";
$result = $conn->query($sql);

$conn->close();

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
      $image_path = $row["image_address"];
      $client_name = $row["client_name"];
      $client_desc = $row["client_desc"];
    $resultee .= '<div class="gallery-cell"><div class="w-full flex justify-center"><img src="'.$image_path.'" class="client-image" /></div></div>';
  }
} 

$resultee .= '</div>';

CSS and JS:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flickity/1.0.0/flickity.css" />
<style>
.gallery {
  width: 93vw;
  background: #e5e5e5;
  margin-left: -5%;
}

.gallery-cell {
  width: 93vw;
  margin-right: 10px;
}
    
   
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flickity/1.0.0/flickity.pkgd.js"><script>

Can anyone please help, I'm unsure what to do

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I had a similar issue with a dynamically loaded flickity carousel where all of my cells were initially stacked on top of each other. It could be two things: you're just adding elements to the html instead of using flickity 'append' to add items to the initialized carousel. Second, if you call 'resize' after on a visible and populated carousel, it should be able to measure and position the elements correctly.

/* init carousel via jquery */
var $carousel = $('.gallery').flickity( { "wrapAround": true } );

/* add item(s) to $carousel */
$carousel.flickity( 'append', your_item);

/* force a redraw */
$carousel.flickity('resize');

API for reference: https://www.tszshan.org/home/new/common/js/flickity-docs/api.html#resize

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda