Last active
August 29, 2015 13:57
-
-
Save chrisvoncsefalvay/9569257 to your computer and use it in GitHub Desktop.
Randomising font sizes and positions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function( $ ) { | |
$.fn.randomise = function(options) { | |
var settings = $.extend({ | |
// Setting defaults | |
position: "absolute", | |
sizemin: 10, | |
sizemax: 64, | |
xmin: 0, | |
ymin: 0, | |
xmax: 640, | |
ymax: 480 | |
}, options); | |
// Loop function to randomise positions | |
// and font-size. | |
return this.each(function() { | |
$(this).css( 'position', settings.position ); | |
var fs = Math.floor(Math.random() * (settings.sizemax - settings.sizemin)) + settings.sizemin; | |
var fx = Math.floor(Math.random() * (settings.xmax - settings.xmin)) + settings.xmin; | |
var fy = Math.floor(Math.random() * (settings.ymax - settings.ymin)) + settings.ymin; | |
$( this ).css('font-size', fs); | |
$( this ).css('top', fx); | |
$( this ).css('left', fy); | |
}); | |
}; | |
}( jQuery )); | |
// Use example, utilising all defaults | |
$('.objects').randomise(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Partly created as a test harness for collision detection.
How to work it:
Initiate
randomise();
with the parameters for font-size, xpos and ypos. Target the desired object through specifying thetarget
- don't forget to specify.
for classes/#
for ids.Update: Thanks to @henrahmagix, this is now a fully fledged jQuery plugin. Enjoy at your heart's delight.