Advertisement
Guest User

Untitled

a guest
Nov 5th, 2016
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.60 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.   <meta charset="utf-8">
  5.   <title>background image aléatoire</title>
  6.   <style>
  7.     /* adapte l'image de fond
  8.     sur la largeur et la hauteur
  9.     de la page html */
  10.     body, html{
  11.         background-size: cover;
  12.         width:100%;
  13.         height:100%;
  14.         padding:0;
  15.         margin:0;
  16.     }
  17.   </style>
  18.   <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  19.   <script>
  20.     var bgImage = [
  21.     'http://img4.hostingpics.net/pics/604727WallpaperHD11F1.jpg',
  22.     'http://img4.hostingpics.net/pics/705951135353683486493.jpg',
  23.     'http://img4.hostingpics.net/pics/413614windowsvista3720px.jpg',
  24.     'http://img4.hostingpics.net/pics/537958amdcomputergamingposterbackground415443.jpg',
  25.     'http://img4.hostingpics.net/pics/698903intelxeonprocessorchess5638602x339.jpg',
  26.     'http://img4.hostingpics.net/pics/7433395WsquLs.png',
  27.     'http://img4.hostingpics.net/pics/697356battlefield3tanksHD1024x576.jpg',
  28.     'http://img4.hostingpics.net/pics/494132alienwarebluelogo579.jpg'
  29.     ]
  30.     $( function() {
  31.         // nombre max d'images que le tableau bgImage contient
  32.         var bgCount = bgImage.length-1;
  33.        
  34.         do{
  35.             // change d'image aléatoirement
  36.             index = Math.floor((Math.random() * bgCount) );
  37.            
  38.             // recommence a changer d'image si celle d'avant etait la meme
  39.         }while( index == localStorage.getItem("lastIndex")  )
  40.        
  41.         //  mémorise la nouvelle image pour comparer
  42.         // au prochain rechargement de la page
  43.         localStorage.setItem("lastIndex",index);
  44.    
  45.         // met une image de fond dans la page html
  46.         $( "body" ).css('background-image', "url("+bgImage[index]+")");
  47.     });
  48.   </script>
  49. </head>
  50. <body>
  51. </body>
  52. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement