Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Fading background images Randomly
Old 06-23-2009, 07:25 PM Fading background images Randomly
Experienced Talker

Posts: 31
Trades: 0
Hi,

I have been looking for a way to get a background image on a site to randomly fade into another image from an array of images.

(for example: there are 5 images in the array and they randomly fade into each other every 10 seconds)

Has anyone done anything similiar to this before in jQuery?

Thanks in advance
jwalker80 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-23-2009, 08:39 PM Re: Fading background images Randomly
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
I haven't done exactly what you're describing, but there's a couple of things you must understand in order to accomplish it. It should be noted that you can't fade background images, only IMG tags, or entire DIVs that have background-images on them. You can't just fade the background-image by itself.

1. To fade two images into another, they should be absolutely positioned in the same place. Either one is behind the other, which fades out, or else both fade in and out simultaneously (this is probably what you want if you don't know what order they'll be in).

2. You need to understand random numbers in JavaScript. Here is a guide I've found useful: Make JavaScript Math.random useful

Other than that, it is just a matter of hiding all of the images to begin with, revealing one randomly, placing a class on it to distinguish it, then choosing from the remaining items in the nodelist to determine which one to fade to. It is probably about an hour job to work out the problems...

Here's a snippet of code that may help you:
Code:
var images = $("#slide img:not(.active)");
var random = Math.floor(Math.random()*images.length);
$(images).eq(random).addClass("changeto");
This assumes that the image currently being displayed has a class of "active" on it, and the image being switched to gets a "changeto" class added to it. Of course, you actually have to do something with these two classes when you do your fade, then switch the active class in the callback, but that's another story...
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 06-23-2009 at 08:51 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 06-24-2009, 06:56 AM Re: Fading background images Randomly
Experienced Talker

Posts: 31
Trades: 0
nice one, ill have a look at doing it that way
jwalker80 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Fading background images Randomly
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.50390 seconds with 12 queries