随机体背景图片 [英] Random body background-image

查看:144
本文介绍了随机体背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着在网络上几个教程,并没有一个似乎正常工作。我正在试图做的是很简单,我认为:

我有我需要随机显示在页面加载9种不同的.jpg图像 - 是背景。这应该是相当简单吧?

谢谢,

编辑(不好意思,忘了附上code - 在网上找到):

  $(文件)。就绪(函数(){    bgImageTotal = 9;    randomNumber = Math.round(的Math.random()*(bgImageTotal-1))+ 1;    imgPath =('../ IMG / BG /+ randomNumber +'JPG。');    $(身体)的CSS(背景图片,('网址('+ imgPath +')'));});


解决方案

看看这个教程:<一href=\"http://briancray.com/2009/12/28/simple-image-randomizer-jquery/\">http://briancray.com/2009/12/28/simple-image-randomizer-jquery/

首先创建一个图像数组:

  VAR图像= ['image1.jpg,image2.jpg','image3.jpg','image4.jpg','image5.jpg'];

然后,设置一个随机图像作为背景图片:

  $('身体')的CSS({'背景图像':'网址(图片/+图像[Math.floor(的Math.random()* images.length )] +')'});

这应该工作没问题。

I've tried several tutorial on the web and none seems to work properly. What I'm trying to do is quite simple I think:

I have 9 different .jpg images that I need to randomly show up on page load - to be background. This should be fairly simple right?

Thanks,

EDIT (Sorry, forgot to attach the code - found in the web):

$(document).ready(function(){

    bgImageTotal=9;

    randomNumber = Math.round(Math.random()*(bgImageTotal-1))+1;

    imgPath=('../img/bg/'+randomNumber+'.jpg');

    $('body').css('background-image', ('url("'+imgPath+'")'));

});

解决方案

Check out this tutorial: http://briancray.com/2009/12/28/simple-image-randomizer-jquery/

First create an array of images:

var images = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg'];

Then, set a random image as the background image:

$('body').css({'background-image': 'url(images/' + images[Math.floor(Math.random() *      images.length)] + ')'});

That should work no problem.

这篇关于随机体背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆