random background image on blogger

寵の児 提交于 2019-12-06 12:27:58

That is how I would do it but as usually, I'm open for edit, comments, ameliorations and suggestions if there's a way to make this code - and me - a better one.

So you wasn't that far cause actually my code look a bit like yours but with the abstraction of manually add the multiplier to the random number generator. With this all you have to do is to set your backgrounds URLs under the imageURLs variable.

Here's the documentation for the .style object.

    <script type="text/javascript">
function backgrounds() {
    var imageURLs = ["https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-301322.jpg", "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-103541.jpg", "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-279000.jpg", "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-106689.jpg", "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-365847.jpg", "https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-284161.jpg"];
    return imageURLs;
}

function randomNumber(max) {
    return Math.floor((Math.random() * max));
}

function getBackground() {
    var numberOfImages = backgrounds().length,
        uniqueNumber = randomNumber(numberOfImages);
    return backgrounds()[uniqueNumber];
}

function showBackground() {
    document.body.style.backgroundImage = "url('" + getBackground() + "')";
    document.body.style.backgroundPosition = "center center";
    document.body.style.backgroundRepeat = "no-repeat";
    document.body.style.backgroundAttachment = "fixed";
}
window.onload = showBackground();
    </script>

[UPDATE 1]

If you have for exemple 10 pictures, then change imageURLs so it's should be something like

var imageURLs = ["http://img1.jpg","http://img2.jpg","http://img3.jpg","http://img4.jpg","http://img5.jpg","http://img6.jpg","http://img7.jpg","http://img8.jpg","http://img9.jpg","http://img10.jpg"];

No need to add the number 10 (in this example) anywhere cause actually the following get it automatically:

var numberOfImages = backgrounds().length;

So in my example, the number 10 (number of pictures I have) which is now obtained is used to generate a random number in range [0,max] with max=10 in our example. So we get the random number with the help of the function :

function randomNumber(max) {
  return Math.floor((Math.random() * max));
}

We call it under showBackground() with the help of:

uniqueNumber = randomNumber(numberOfImages);

Once we have uniqueNumber, we use it to get the url of one image stored under the function backgrounds() with :

return backgrounds()[uniqueNumber];

[UPDATE 2]

To continue with explanations, you have to add the script at the end of the the template HTML just before </html> (just tested it).

[UPDATE 3]

To achieve what you wanted to do with :

document.write("<style>");
document.write("body {");
document.write(' background:url("' + image[random] + '") no-repeat fixed center center;');
document.write(" }");
document.write("</style>");

we use the function showBackground()

And finaly to fix what you said which was:

The background image is the very last element to be loaded upon refresh

I added the following to the script:

window.onload = showBackground();

Not solved (30/12/2016 18:48 - CET)

I currently have no mac or windows with me to test or to try to fix:

It doesn't seem to work on Safari

but if someone want to help here is the blogger I use to test.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!