Change background with js

Asked

Viewed 477 times

0

Well, I have an image for demonstration

http://prntscr.com/8xu33k

When a user clicks on one, the bottom of the body will change according to what he chose. But in this case the background is an image that corresponds to those colors. How can I do this?

2 answers

0

Good with javascript is more difficult... I will use jquery if you want to use !

<div id="azul">azul</div>
<br>
<div id="vermelho">vermelho</div>
<br>
<div id="cinza">cinza</div>
<br>

Well just you read and code n is hard... anything talks

$("#azul").click(function() {
      $("body").css({
        "background": "blue";
     });

      $("#cinza").click(function() {
        $("body").css({
          "background": "#ccc";
        });

        $("#vermelho").click(function() {
          $("body").css({
            "background": "red";
          });
        });

EDIT: Leandro, don’t forget to put ";" to close. This is important.

  • Got it. I thought this was js kkk I confusing the two

  • But how do I put on picture?

  • simple - instead of #ccc or the color vc puts the path of the image type "background": "url(img.png)" excuse the delay because I am programming.

0


Basis of how you want adapting the code of Leandro:

    $("#azul").click(function() {
    $("body").css({
    "background": "url(azul.png)";
    });

    $("#cinza").click(function() {
    $("body").css({
    "background": "url(cinza.png)";
    });

    $("#vermelho").click(function() {
    $("body").css({
    "background": "url(vermelho.png)";
    });
    });
  • vlw bro, wanted it right, thanks leandro tbm

Browser other questions tagged

You are not signed in. Login or sign up in order to post.