Changer une propiété css via javascript

Résolu
gibnem -  
gibnem Messages postés 28 Statut Membre -
Bonjour,
voilà .. je voulait changer l'image et le couleur du body en java script suivant la résolution de l'intérnaute
voila mon script:

<script language="JavaScript">
<!--
var largeur;
largeur = screen.width;

if(largeur < 1200){
document.getElementById(‘body’).style.background = ‘#0012ff url(images/30.png) no-repeat right bottom fixed’;
}
else{
document.getElementById(‘body’).style.background = ‘#2746E0 url(images/3.png) no-repeat right bottom fixed’;
}
//-->
</script>

je sais pas pouquoi ça n'a pas marché apparemment tt est bien mé.........je sais pas
si vous avait des proposition.......
Configuration: Windows XP
Firefox 3.0.6

7 réponses

  1. Alain_42 Messages postés 5413 Statut Membre 904
     
    as tu mis dans ta page <body id="body"> ?

    sinon tu peux esayer:

    document.body.style.background = ....
    
    0
  2. gibnem
     
    en faite dans ma page de style css ila ya
    body {

    background: #0012ff url(images/30.png) no-repeat right bottom fixed;
    margin-bottom: 20px;
    }
    0
  3. gibnem
     
    j'ai essayé comme ce que ta dit

    if(largeur < 1200){
    document.body.style.background = #0012ff url(images/30.png) no-repeat right bottom fixed;
    }
    else{
    document.body.style.background = #2746E0 url(images/3.png) no-repeat right bottom fixed;
    }
    mais ça n'a pas marché!!
    0
  4. Alain_42 Messages postés 5413 Statut Membre 904
     
    et:

    if(largeur < 1200){
        body.style.background = #0012ff url(images/30.png) no-repeat right bottom fixed;
    }
    else{
          body.style.background = #2746E0 url(images/3.png) no-repeat right bottom fixed;
    } 
    0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. Alain_42 Messages postés 5413 Statut Membre 904
     
    pardon:

    if(largeur < 1200){
        body.style.background =" #0012ff url(images/30.png) no-repeat right bottom fixed";
    }
    else{
          body.style.background =" #2746E0 url(images/3.png) no-repeat right bottom fixed";
    } 
    
    
    0
  7. Marco la baraque Messages postés 1030 Statut Contributeur 329
     
    Bonsoir,
    Essaie en ajoutant des double-quote :
    document.body.style.background = "#0012ff url(images/30.png) no-repeat right bottom fixed"; 


    Cordialement,
    0
  8. gibnem Messages postés 28 Statut Membre 8
     
    bon merci a vous tous
    voici le code final et ça marche cristal XD

    if(largeur < 1200){
    document.body.style.background = "#0012ff";
    document.body.style.backgroundImage='url(images/30.png)';
    document.body.style.backgroundPosition = 'right bottom';
    document.body.style.backgroundRepeat = 'no-repeat';
    document.body.style.backgroundAttachment = 'fixed';
    }
    else{
    document.body.style.background = "#2746E0";
    document.body.style.backgroundImage='url(images/3.png)';
    document.body.style.backgroundPosition = 'right bottom';
    document.body.style.backgroundRepeat = 'no-repeat';
    document.body.style.backgroundAttachment = 'fixed';
    }
    0