English 中文(简体)
Rand 身体背景评价
原标题:Random body background-image

我在网上尝试过几次辅导,似乎都没有适当工作。 我想,我想要做的是很简单:

我有9种不同的jpg图像,我需要随机展示,以示背景。 这应当是相当简单的权利?

感谢

EDIT (Sorry, forgot to annex 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/

首先是制作一系列图像:

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

然后,将随机图像作为背景形象:

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

That should work no problem.

问题回答

using jQuery: $("body").css("background-image", "url(" + Math.floor(Math.random()*9) + ".jpg)");

假设你们的图像在8.jpg之前就被点名,并且与你的网页相同。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签