在页面刷新时随机更改HTML背景

时间:2013-09-22 23:46:56

标签: javascript jquery html css

我知道这个主题已被大量查看,但我希望我的主页随机选择一个图像,然后更改该图像的字幕。我的网站的网址是:http://www.connorloughlin.com

页面底部有一个小字幕,如果相关背景发生变化则会很棒。如果它太复杂我不会打扰,但认为它看起来很好!我最好有5张图片,每张图片都有自己的副标题。

如果您希望我澄清任何事情并提前致谢,请告诉我们。

3 个答案:

答案 0 :(得分:1)

以下是JQuery: 只需为每个图像命名为bg1或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);
}

这不是编写代码的最干净,最语义的方法。但希望它会让你开始朝着正确的方向前进。

答案 1 :(得分:1)

最简单的方法,在纯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

或者,如果您希望每隔 n 毫秒更改背景,则可以添加以下内容:

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

JS Fiddle demo

显然,它会每5000毫秒更改一次图像(和副标题)。

答案 2 :(得分:1)

由于您正在使用jQuery,我使用了http://jsbin.com/OQugAMI/4/edit

创建了一个版本

1)创建一个包含图像列表的数组。字幕

    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)从该数组中选择一个随机图像

/**
 * 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)更新H4& body CSS反映了选择

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

这将选择一个新的图像&每页加载字幕