ConnorL
ConnorL

Reputation: 227

Change HTML Background randomly on page refresh

I know this topic has been looked at loads of times, but I would like my home page to randomly select an image and then change the subtitle for that image. The URL for my site is: http://www.connorloughlin.com

At the bottom of the page is a little subtitle, would be great if it changed for the relevant background. If it's too complex I won't bother but thought it'd look good! Preferably i'd have 5 images each with their own subtitle.

Let me know if you want me to clarify anything and thanks in advance!

Upvotes: 0

Views: 6931

Answers (3)

David Thomas
David Thomas

Reputation: 253318

The simplest way, in plain JavaScript:

var images = [
    {
        subtitle : 'Subtitle text for image one...',
        src : 'http://placekitten.com/1000/1000'
    },
    {
        subtitle : 'Subtitle text for image two...',
        src : 'http://lorempixel.com/1000/1000/people/'
    },
    {
        subtitle : 'Subtitle text for image three...',
        src : 'http://lorempixel.com/1000/1000/nightlife/'
    },
    {
        subtitle : 'Subtitle text for image four...',
        src : 'http://lorempixel.com/1000/1000/nature/'
    }
];

function setBackground (images) {
    // generates a random integer between 0 and the length of the supplied array:
    var n = Math.floor(Math.random() * images.length),
        // works out whether to use the 'textContent' or 'innerText' property:
        textProperty = 'textContent' in document ? 'textContent' : 'innerText';

    // sets the background-image of the 'body' element:
    document.body.style.backgroundImage = 'url(' + images[n].src + ')';

    // sets the text of the relevant subtitle element:
    document.getElementById('subtitleElementID')[textProperty] = images[n].subtitle;
}

setBackground(images);

JS Fiddle demo.

Or, if you'd rather change the background every n milliseconds, you could add the following:

window.setInterval(function(){
    setBackground(images)
}, 5000);

JS Fiddle demo.

Which, obviously, will change the image (and subtitle) every 5000 milliseconds.

Upvotes: 1

providencemac
providencemac

Reputation: 622

Since you're using jQuery, I've made a version using that: http://jsbin.com/OQugAMI/4/edit

1) create an Array containing the list of images & subtitles

    var backgrounds = [
  {  image: 'http://www.connorloughlin.com/images/background.jpg',
   subtitle: 'Looking out at Carcassonne, France - August 2013'
  },
  {  image: 'http://upload.wikimedia.org/wikipedia/commons/1/13/1632x1224_sertaoe_2_rio_grande_do_norte_landscape_panorama_brasil.jpg',
   subtitle: 'Version 2'
  },
  {  image: 'http://upload.wikimedia.org/wikipedia/commons/7/71/1632x1224_sertaoe_rio_grande_do_norte_landscape_panorama_brasil.jpg',
   subtitle: 'Version 3'
  }
       ]; 

2) select a random image from that array

/**
 * Returns a random integer between min and max
 * Using Math.round() will give you a non-uniform distribution!
 * http://stackoverflow.com/questions/1527803/generating-random-numbers-in-javascript-in-a-specific-range
 */
function getRandomInt (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

$(document).ready(function(){
    var bgNumber = getRandomInt(0, backgrounds.length-1);
}

3) update the H4 & body CSS to reflect the choice

$('body').css('background-image', 'url('+backgrounds[bgNumber].image+')');
$('h4').html(backgrounds[bgNumber].subtitle);

This will pick a new image & subtitle on each page load

Upvotes: 1

Andrew Ice
Andrew Ice

Reputation: 841

The following is in JQuery: Just name each of your images bg1 or bg2

//  On Page Load
  backgroundSelect();

function backgroundSelect(){
  var sub1 = "this is my subtitle"

  var numImgs = 5;  // The Number of images you have total.
  var select  = Math.round(Math.random() * numImgs) + 1;  // add one so not zero based.

  $('body').css('background-image', 'bg' + select);
  $('subtitle_element').replaceWith(sub1);
}

This is not the cleanest and most semantic way to write your code. But hopefully it will get you started in the right direction.

Upvotes: 1

Related Questions