Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>background image aléatoire</title>
- <style>
- /* adapte l'image de fond
- sur la largeur et la hauteur
- de la page html */
- body, html{
- background-size: cover;
- width:100%;
- height:100%;
- padding:0;
- margin:0;
- }
- </style>
- <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
- <script>
- var bgImage = [
- 'http://img4.hostingpics.net/pics/604727WallpaperHD11F1.jpg',
- 'http://img4.hostingpics.net/pics/705951135353683486493.jpg',
- 'http://img4.hostingpics.net/pics/413614windowsvista3720px.jpg',
- 'http://img4.hostingpics.net/pics/537958amdcomputergamingposterbackground415443.jpg',
- 'http://img4.hostingpics.net/pics/698903intelxeonprocessorchess5638602x339.jpg',
- 'http://img4.hostingpics.net/pics/7433395WsquLs.png',
- 'http://img4.hostingpics.net/pics/697356battlefield3tanksHD1024x576.jpg',
- 'http://img4.hostingpics.net/pics/494132alienwarebluelogo579.jpg'
- ]
- $( function() {
- // nombre max d'images que le tableau bgImage contient
- var bgCount = bgImage.length-1;
- do{
- // change d'image aléatoirement
- index = Math.floor((Math.random() * bgCount) );
- // recommence a changer d'image si celle d'avant etait la meme
- }while( index == localStorage.getItem("lastIndex") )
- // mémorise la nouvelle image pour comparer
- // au prochain rechargement de la page
- localStorage.setItem("lastIndex",index);
- // met une image de fond dans la page html
- $( "body" ).css('background-image', "url("+bgImage[index]+")");
- });
- </script>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement