Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Javascript] Fonction rand sur backgroundColor

Sujet résolu
    28 mai 2010 à 11:07:58

    Bonjour, voilà je débute en javascript et je voudrais faire une fonction pour changer la couleur du background, trouvez vous le problème ?

    function bg(){
    	var color = new array("red","yellow","black","green","blue");
    	body.style.backgroundColor = Math.random() color.length
    }
    


    <body onload="bg">
    
    • Partager sur Facebook
    • Partager sur Twitter
    http://www.icecom.fr : Agence de création de site internet & marketing digital
      28 mai 2010 à 11:16:22

      <couleur nom="orange">body.style.backgroundColor = Math.round(Math.random() color.length)</couleur>
      


      Pas bon.

      Math.random(color.length) sera plus adapté.

      Donc au final :

      body.style.backgroundColor = color[Math.round(Math.random(color.length))];
      
      • Partager sur Facebook
      • Partager sur Twitter
        28 mai 2010 à 11:19:17

        Ca ne marche toujours pas. :euh:
        • Partager sur Facebook
        • Partager sur Twitter
        http://www.icecom.fr : Agence de création de site internet & marketing digital
          28 mai 2010 à 11:30:43

          Je me suis permis de le refaire :
          <html>
          <head>
          <script type='text/javascript'>
          var bg = function () {
          	var color = ["red","yellow","black","green","blue"];
          	document.getElementById('machin').style.backgroundColor =  color[Math.round(Math.random(color.length))];
          }
          </script>
          </head>
          <body onload="javascript:bg();">
          <div id='machin'>poum</div>
          </body>
          </html>
          
          • Partager sur Facebook
          • Partager sur Twitter
            28 mai 2010 à 11:35:04

            Je viens de le recopier mot pour mot et ça ne marche toujours pas.
            • Partager sur Facebook
            • Partager sur Twitter
            http://www.icecom.fr : Agence de création de site internet & marketing digital
              28 mai 2010 à 11:37:29

              Hum... je vais avoir du mal à t'aider, tout tourne parfaitement sur mon pc avec ce code ><
              • Partager sur Facebook
              • Partager sur Twitter
                28 mai 2010 à 11:40:05

                ah c'est bon ça marche j'avais oubliais je mettre un id à mon body... Par contre je remarque que seulement les deux premières couleur se charge...

                Merci !
                • Partager sur Facebook
                • Partager sur Twitter
                http://www.icecom.fr : Agence de création de site internet & marketing digital
                  28 mai 2010 à 12:54:38

                  document.getElementById('machin').style.backgroundColor =  color[Math.round(Math.random()*color.length)];
                  

                  En fait. :-°
                  Pense à mettre ton sujet en résolu ;)

                  Je me permets un petit "Edit" d'explication. Math.random génère en fait un chiffre aléatoire entre 0 et 1. Math.round permet d'arrondir le nombre obtenu. Néanmoins, Math.floor serait plus avantageux pour une égalité approximative des chances...
                  • Partager sur Facebook
                  • Partager sur Twitter
                    28 mai 2010 à 13:55:12

                    D'accord merci !
                    • Partager sur Facebook
                    • Partager sur Twitter
                    http://www.icecom.fr : Agence de création de site internet & marketing digital

                    [Javascript] Fonction rand sur backgroundColor

                    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                    • Editeur
                    • Markdown