Created
October 2, 2011 16:08
-
-
Save qrush/1257579 to your computer and use it in GitHub Desktop.
coffeescript vs javascript. rewriting http://www.spookandpuff.com/examples/dynamicTextSize.html
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
$( document ).ready( function() { | |
var $body = $('body'); //Cache this for performance | |
var setBodyScale = function() { | |
var scaleFactor = 0.35, | |
scaleSource = $body.width(), | |
maxScale = 600, | |
minScale = 30; | |
var fontSize = scaleSource * scaleFactor; //Multiply the width of the body by the scaling factor: | |
if (fontSize > maxScale) fontSize = maxScale; | |
if (fontSize < minScale) fontSize = minScale; //Enforce the minimum and maximums | |
$('body').css('font-size', fontSize + '%'); | |
} | |
$(window).resize(function(){ | |
setBodyScale(); | |
}); | |
//Fire it when the page first loads: | |
setBodyScale(); | |
}); |
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
# based on http://www.spookandpuff.com/examples/dynamicTextSize.html | |
class BodyScaler | |
scaleFactor: 0.35 | |
maxScale: 400 | |
minScale: 30 | |
constructor: -> | |
@body = $("body") | |
scale: => | |
scaleSource = @body.width() | |
fontSize = scaleSource * @scaleFactor | |
if (fontSize > @maxScale) | |
fontSize = @maxScale | |
if (fontSize < @minScale) | |
fontSize = @minScale | |
@body.css("font-size", "#{fontSize}%") | |
$ -> | |
scaler = new BodyScaler() | |
scaler.scale() | |
$(window).resize(scaler.scale) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also
is shorthand for