Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 06/02/2013, à 22:52

max02100

Background aléatoire JQuery

Bonsoir à tous, je bosse actuellement sur mon petit site et perso et je suis parti sur un background aléatoire en jquery.
J'ai donc codé ce petit script

var images = ['/var/www/MediaLink/background/1.jpg', '/var/www/MediaLink/background/2.jpg', '/var/www/MediaLink/background/3.jpg', './/var/www/MediaLink/background/4.jpg'];
$('body').css({'background-image': 'url(images/' + images[Math.floor(Math.random() * images.length)] + ')'});

Cependant, cela n'affecte pas du tout mon background qui reste blanc sad malgré le chargement de jquery et du script en question

<!DOCTYPE html>
<html lang="fr"> 
  <head> 
    <meta charset="UTF-8">
    <title>CV en ligne</title>
	
	<!-- meta -->
    <meta name="MediaMax" content="">
 
    <!-- mon icon -->
    <link rel="shortcut icon" href="favicon.ico">
	
	<!-- mon template.css -->
    <link rel="stylesheet" href="index.css" type="text/css"  media="screen">
    
	<!-- importation de jquery -->
    <script type="text/javascript" src="script/jquery.js"></script>
    
	<!-- importation des scripts -->
	<script type="text/javascript" src="script/activebg.js"></script>
	
  </head>
  		<body>

Si quelqu'un à la réponse à ma solution ca serait magnifique big_smile

Hors ligne

#2 Le 07/02/2013, à 01:10

Maisondouf

Re : Background aléatoire JQuery

c'est pas plutôt ça la syntaxe de css()...

$('body').css('background', "url('monimage.png')");

Hésites pas à mixer le ' et les " pour faciliter la compréhension par le navigateur.

Pour toi ça devrait donner à peu près ça:

$('body').css('background-image', "url(images/" + images[Math.floor(Math.random() * images.length)] + ")");

ASUS M5A88-v EVO avec AMD FX(tm)-8120 Eight-Core Processor,  OS principal Precise 12.04.1 LTS 63bits½
Bricoleur, menteur, inculte, inadapté social et mythomane, enfin d'après certains....
"the secret of my form is summed up in two words, no sport" (Winston Churchill)

Hors ligne

#3 Le 07/02/2013, à 09:01

max02100

Re : Background aléatoire JQuery

hé bien corrigé selon tes dires en changeant images/ en background/ car mes images sont dans un dossier background et ca ne marche pas non plus

var images = ['1.jpg', '2.jpg', '3.jpg', '4.jpg'];
$('body').css('background-image', "url(background/" + images[Math.floor(Math.random() * images.length)] + ")");

Hors ligne

#4 Le 07/02/2013, à 11:02

Maisondouf

Re : Background aléatoire JQuery

Ton code est placé où par rapport à la balise <body> ?


ASUS M5A88-v EVO avec AMD FX(tm)-8120 Eight-Core Processor,  OS principal Precise 12.04.1 LTS 63bits½
Bricoleur, menteur, inculte, inadapté social et mythomane, enfin d'après certains....
"the secret of my form is summed up in two words, no sport" (Winston Churchill)

Hors ligne

#5 Le 07/02/2013, à 11:25

max02100

Re : Background aléatoire JQuery

alors mon code jquery est placé dans un fichier activebg.js qui lui est appelé dans mon head de l'index.php.
Le scripte est rangé dans un dossier script

Hors ligne

#6 Le 07/02/2013, à 11:29

Maisondouf

Re : Background aléatoire JQuery

Tu mets ton tableau dans le <head>

<script type="text/javascript">
var images = ['1.jpg', '2.jpg', '3.jpg', '4.jpg'];
</script>

Et ton code juste avant la fermeture de </body>

<script type="text/javascript">
$(document).ready(function(){ 
  var num=Math.floor(Math.random() * images.length);
  $('body').css('background-image', "url('images/" + images[num] + "')");
});
</script>

EDIT : ça marche très bien chez moi : http://home.maisondouf.fr/body-pic.php

Dernière modification par Maisondouf (Le 07/02/2013, à 11:32)


ASUS M5A88-v EVO avec AMD FX(tm)-8120 Eight-Core Processor,  OS principal Precise 12.04.1 LTS 63bits½
Bricoleur, menteur, inculte, inadapté social et mythomane, enfin d'après certains....
"the secret of my form is summed up in two words, no sport" (Winston Churchill)

Hors ligne

#7 Le 07/02/2013, à 14:14

max02100

Re : Background aléatoire JQuery

en effet ca marche mais pourquoi mon ancien script marche pas alors que c'est le meme ???

Hors ligne

#8 Le 07/02/2013, à 15:29

Maisondouf

Re : Background aléatoire JQuery

À cause de çà

"url(' ..... ')"

Tu n'avais pas d'apostrophes pour encadrer l'url.

"url(background

ASUS M5A88-v EVO avec AMD FX(tm)-8120 Eight-Core Processor,  OS principal Precise 12.04.1 LTS 63bits½
Bricoleur, menteur, inculte, inadapté social et mythomane, enfin d'après certains....
"the secret of my form is summed up in two words, no sport" (Winston Churchill)

Hors ligne