Randomize an element upon page refresh
Does anyone know how to utilize jQ开发者_JS百科uery to Randomize (eg. a flash video) when a user comes to a page? would I use http://plugins.jquery.com/project/random_element this plugin?
Please help!
Hmmm, I'd not take this approach because I think this plugin requires all the elements to be present on the page. So if you have 10 flash videos to randomise then you'll have to load them all onto the page making for a potentially large page or (actually) long page load times.
Given you are doing this on page load, wouldn't you simply pick the random item in your code behind and send only the one flash video to the page?
I like Schattenbaum's answer, it handles the issue of randomly showing a child but it doesn't solve the problem of having to load your flash movies up front and slowing down the page. Here is something I have found useful for loading/unloading flash files: http://jquery.thewikies.com/swfobject/examples.
First you would have to download that swfobject plugin, then you would load up the flash using .flash() instead of .show().
For example:
function getRandomElement() {
var amount = $('body').children().length;
var randomNumber = Math.floor(Math.random()*amount);
return $('body').children().get(randomNumber);
}
...
var random = getRandomElement();
random.flash({swf: 'flash_file.swf',height: 100,width: 100});
Depending on what you are doing, you might also have to hide or unload the movie with a timer once it is completed.
You can try it with the children function of jquery.
Example:
function getRandomElement() {
var amount = $('body').children().length;
var randomNumber = Math.floor(Math.random()*amount);
return $('body').children().get(randomNumber);
}
UPDATE
I don't know if I understand you right. but when you want to show a random element, do it like this:
var random = getRandomElement();
random.show();
精彩评论