Skip to content

Refactored #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 28 additions & 29 deletions jquery.fittext.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
/*global jQuery */
/*!
/*jshint jquery:true strict:false*/
/*!
* FitText.js 1.0
*
* Copyright 2011, Dave Rupert http://daverupert.com
* Released under the WTFPL license
* Released under the WTFPL license
* http://sam.zoy.org/wtfpl/
*
* Date: Thu May 05 14:23:00 2011 -0600
*/

(function( $ ){
(function($){
var defaults = {
'minFontSize' : -9e99,
'maxFontSize' : +9e99
};

// Resizer() resizes items based on the object width divided by the compressor * 10
function resizer (self, settings) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I factored this out of the plugin execution so that it doesn't need to be defined each time the plugin is called.

return function () {
self.css('font-size', Math.max(Math.min(
self.width() / (settings.compressor * 10),
parseFloat(settings.maxFontSize)),
parseFloat(settings.minFontSize)));
};
}

$.fn.fitText = function( kompressor, options ) {

$.fn.fitText = function(compressor, options) {
// Setup options
var compressor = kompressor || 1,
settings = $.extend({
'minFontSize' : Number.NEGATIVE_INFINITY,
'maxFontSize' : Number.POSITIVE_INFINITY
}, options);

return this.each(function(){

// Store the object
var $this = $(this);

// Resizer() resizes items based on the object width divided by the compressor * 10
var resizer = function () {
$this.css('font-size', Math.max(Math.min($this.width() / (compressor*10), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize)));
};
var settings = $.extend({'compressor' : compressor || 1}, defaults, options);

// Call once to set.
resizer();
// Call on resize. Opera debounces their resize by default.
$(window).on('resize', resizer);
});
return this
.each(function(){
// Call on resize. Opera debounces their resize by default.
$(window)
.on('resize', resizer($(this), settings))
.trigger('resize');
});

};

})( jQuery );
}(jQuery));