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

179
Visualizações
Gallery is not interactive while using JavaScript and Css

I have built a gallery that supposes to change the main picture when other one is chosen from the small menu down below, however, it does not react when i click on other small photos it just stays the same, could anyone please help with it

// image gallery
const changeimage = (src) => {
  document.getElementById("main").src = src
}
.gallery {
  width: 90%;
  margin: 0 auto;
}

.gallery-main {
  width: 45rem;
  height: 30rem;
  margin: 0 auto;
}

.gallery-main img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
}

.gallery-p {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 2rem;
}

.gallery-p img {
  width: 9rem;
  height: 7rem;
  object-fit: cover;
  border-radius: 4px;
  margin: 0.5rem;
  cursor: pointer;
}
<div class="gallery">
  <div class="gallery-main">
    <img src="images/dublin culture1.jpg" alt="" id="main">
  </div>

  <div class="gallery-p">
    <img src="images/dublin culture2.jpg" alt="" onclick="changeimage(this.src)">
    <img src="images/dublin culture3.jpg" alt="" onclick="changeimage(this.src)">
    <img src="images/dublin culture4.jpg" alt="" onclick="changeimage(this.src)">
    <img src="images/dublin people1.jpg" alt="" onclick="changeimage(this.src)">
    <img src="images/dublin people2.jpg" alt="" onclick="changeimage(this.src)">
    <img src="images/dublin people3.jpg" alt="" onclick="changeimage(this.src)">
  </div>
</div>

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

0

Try this!

document.getElementById("main").src = "https://i.ibb.co/2kfJ15Z/image-cropped.jpg"

const changeimage = (src) => {
    document.getElementById("main").src = src
}
.gallery {
  width: 90%;
  margin: 0 auto;
}

.gallery-main {
  width: 45rem;
  height: 30rem;
  margin: 0 auto;
}

.gallery-main img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
}

.gallery-p {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 2rem;
}

.gallery-p img {
  width: 9rem;
  height: 7rem;
  object-fit: cover;
  border-radius: 4px;
  margin: 0.5rem;
  cursor: pointer;
}
<div class="gallery">
  <div class="gallery-main">
    <img src="images/dublin culture1.jpg" alt="" id="main">
  </div>

  <div class="gallery-p">
    <img src="https://i.ibb.co/2kfJ15Z/image-cropped.jpg" alt="" onclick="changeimage(this.src)">
    <img src="https://i.ibb.co/0GwkygJ/imagename-2.png" alt="" onclick="changeimage(this.src)">
    <img src="https://i.ibb.co/2kfJ15Z/image-cropped.jpg" alt="" onclick="changeimage(this.src)">
    <img src="https://i.ibb.co/0GwkygJ/imagename-2.png" alt="" onclick="changeimage(this.src)">
    <img src="https://i.ibb.co/2kfJ15Z/image-cropped.jpg" alt="" onclick="changeimage(this.src)">
    <img src="https://i.ibb.co/0GwkygJ/imagename-2.png" alt="" onclick="changeimage(this.src)">
  </div>
</div>

Thanks and best regards!

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