$(document).ready(function () {
var myRand = function (top) {
/* generate whole number random from 1 to n */
return Math.floor((Math.random() * top) + 1);
}, wnd = $(window),
i,
y = 50;
/* generate some random circles on the screen */
for (i = 0; i < 20; i++) {
$('body').append(
'<div class="sprite size' + myRand(4) + ' color' + myRand(4) + '" style="top: ' + y + 'px; left: ' + (10 + myRand(wnd.width() - 180)) + 'px;" data-speed="' + myRand(4) + '" data-y="' + y + '"></div>');
y += myRand(150);
}
/*
* generate a final div at the bottom so the body height
* does not change as sprites are moved
*/
$('body').append('<div class="final" style="top: ' + y + 'px;"> </div>');
/* scroll event */
wnd.scroll(function () {
/* loop through all the circle sprites */
$('.sprite').each(function () {
/* Move the sprite based on speed and scroll calculation */
var sprite = $(this),
yPos = -(wnd.scrollTop() / sprite.data('speed'));
yPos = Math.floor(yPos) + sprite.data('y');
sprite.css('top', yPos + 'px');
}); // sprites
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.