$(function() {
var $toggle = $('.christmas-toggle'),
$body = $('body'),
$window = $(window),
speed = 0.1,
horizontalVariance = 100,
createInterval = 100,
doChristmas = function() {
var wndHeight = $window.height(), // Get wind. dimensions often since the user may resize the browser.
wndWidth = $window.width(),
vertSpeed = speed * wndHeight,
startX = Math.random() * wndWidth,
endX = startX + ((Math.random() - 0.5) * horizontalVariance),
$snowFlake = $('<div>', {
text: "*",
addClass: "snow-flake"
});
if (!$toggle[0].checked) {
return;
}
$body.prepend($snowFlake);
$snowFlake.css({
left: startX
}).animate({
left: endX,
top: wndHeight,
opacity: 0
}, wndHeight / speed, 'linear', function() {
$snowFlake.remove();
});
setTimeout(doChristmas, createInterval);
};
$toggle.click(doChristmas);
doChristmas();
});
<label>
<input type="checkbox" class="christmas-toggle" name="christmas" checked />
christmas
</label>
.snow-flake {
position: fixed;
top: -10;
}