var $bubble = $('.bubble'); function bubbles() { // Settings // Calculate a random number of bubbles based on our min/max var bubbleCount = min_bubble_count + Math.floor(Math.random() * (max_bubble_count + 1)); // Create the bubbles for (var i = 0; i < bubbleCount; i++) { $bubble.append('<div class="bubble-container"><div class="bubble"></div></div>'); } // Now randomise the various bubble elements $bubble.find('.bubble-container').each(function(){ // Randomise the bubble positions (0 - 100%) var pos_rand = Math.floor(Math.random() * 101); // Randomise their size var size_rand = min_bubble_size + Math.floor(Math.random() * (max_bubble_size + 1)); // Randomise the time they start rising (0-15s) var delay_rand = Math.floor(Math.random() * 16); // Randomise their speed (3-8s) var speed_rand = 3 + Math.floor(Math.random() * 9); // Random blur var blur_rand = Math.floor(Math.random() * 3); // Cache the this selector var $this = $(this); // Apply the new styles $this.css({ 'left' : pos_rand + '%', '-webkit-animation-duration' : speed_rand + 's', '-moz-animation-duration' : speed_rand + 's', '-ms-animation-duration' : speed_rand + 's', 'animation-duration' : speed_rand + 's', '-webkit-animation-delay' : delay_rand + 's', '-moz-animation-delay' : delay_rand + 's', '-ms-animation-delay' : delay_rand + 's', 'animation-delay' : delay_rand + 's', '-webkit-filter' : 'blur(' + blur_rand + 'px)', '-moz-filter' : 'blur(' + blur_rand + 'px)', '-ms-filter' : 'blur(' + blur_rand + 'px)', 'filter' : 'blur(' + blur_rand + 'px)', }); $this.children('.bubble').css({ 'width' : size_rand + 'px', 'height' : size_rand + 'px' }); }); } // In case users value their laptop battery life // Allow them to turn the bubbles off $('.bubble-toggle').click(function(){ if($bubble.is(':empty')) { bubble(); $bubble.show(); $(this).text('Bubbles Off'); } else { $bubble.fadeOut(function(){ $(this).empty(); }); $(this).text('Bubble On'); } return false; }); bubble();
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)