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

263
Visualizações
How do I click an image to make it my page background?

So, in short, I am a bit new to most of this. What I need to do is make a panel of four different images. I have already done that, and now I need to make it so whatever image I click on will become the background of the page. I'm sorry if I have explained this poorly, but does anybody know how I would code this in javascript? If you could help me, I would greatly appreciate it.

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

0

Assign the source of the image to the background-image property of the document body.

// Attach an event listener to the container, and
// add a listener to it that calls the `handleClick` function.
const images = document.querySelector('.images');
images.addEventListener('click', handleClick, false);

// Check to see if the element ciicked on is an
// image, and then assign the image src to the
// background-image property of the document body
function handleClick(e) {
  if (e.target.matches('img')) {
    const { src } = e.target;
    document.body.style.backgroundImage = `url(${src})`;
  }
}
body { background-size: cover; background-repeat: no-repeat; }
img:hover { cursor: pointer; }
<div class="images">
  <img src="https://dummyimage.com/75x75/000/fff&text=1" />
  <img src="https://dummyimage.com/75x75/575/fff&text=2" />
  <img src="https://dummyimage.com/75x75/a8d/fff&text=3" />
  <img src="https://dummyimage.com/75x75/bbc/fff&text=4" />
</div>

Additional documentation

  • Destructuring assignment

  • Event delegation

  • matches

  • Template/string literals

about 4 years ago · Juan Pablo Isaza Relatório

0

This not the best solution but i think it can help you

    <html>
    <body>
    /*You can make img click able and onclick like button*/
    <img src="this is picture path" onclick="function">
    <script>
    /* Function change background*/
    function function() {
    /* Command js and picture path inside 'this' and url with css command inside "this" actually just copy the code, no-repeat right top just a some css setting */
      document.body.style.background = "url('img_tree.png') no-repeat right top";
/* You see "body" tag right? It change body background*/
    }
    </script>
    
    </body>
    </html>
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