javascript - Refreshing page with a new image -
this question has answer here:
- load random images directory 5 answers
i have stagnant image on site:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="/stylesheets/normalize.css" media="all" rel="stylesheet" /> <link href="/stylesheets/master.css" media="all" rel="stylesheet" /> <script> window.onload = function () { var images = [], i=1, indeximages = true, prefix = '../image/', extension = '.jpg'; while (indeximages) { var = new xmlhttprequest(); a.open('get', prefix+i+extension, false); a.send(); if (a.status != 404) { += 1; images.push(prefix+i+extension); } else { indeximages = false; localstorage['backgroundindex'] = !localstorage['backgroundindex']?0:+localstorage['backgroundindex']+2>images.length?0:+localstorage['backgroundindex']+1; document.body.style.backgroundimage = 'url(' + images[+localstorage['backgroundindex']] + ')'; } } } </script> <style> body { background-repeat: no-repeat; background-attachment: fixed; background-position: center; background-color: black; border-bottom: 8px solid #7d8a28; } </style> </head> <body> <section id="card"> </section> </body> </html> it's want different image each time page refreshes, auto changes 2.jpg, 3.jpg, 10.jpg, whatever. (there hundreds choose from)
could me out solution? i'm not @ this, , first site.
thanks.
you can javascript. define array images have , create random number every time page loads. use random number access array's index read name of image. so:
var images = ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg"]; var randomnumber = math.floor((math.random() * images.length)); //images[randomnumber] contains random image hope helps. regards,
Comments
Post a Comment