随机体背景图像 [英] Random body background-image
本文介绍了随机体背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在网上尝试了几个教程,但似乎没有一个可以正常工作.我想我想做的很简单:
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:
我需要在页面加载时随机显示 9 张不同的 .jpg 图像 - 作为背景.这应该很简单吧?
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?
谢谢,
编辑(抱歉,忘记附上代码 - 在网络上找到):
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+'")'));
});
推荐答案
查看本教程: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)] + ')'});
应该没问题.
这篇关于随机体背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文