(function ($) { var INITIALIZER_ATTR = 'data-fitFontSize'; var INITIALIZER_SELECTOR = '[' + INITIALIZER_ATTR + ']'; var RELATIVE_TO_ELEMENT_ATTR = INITIALIZER_ATTR + '-relativeToElement'; var RELATIVE_TO_ATTR_ATTR = INITIALIZER_ATTR + '-relativeToAttribute'; var PERCENTAGE_ATTR = INITIALIZER_ATTR + '-percentage'; var SCALING_TYPE_ATTR = INITIALIZER_ATTR + '-scaling'; var SCALING_TEXT_ATTR = INITIALIZER_ATTR + '-scalingText'; var SCALING_TEXT_SELECTOR = '[' + SCALING_TEXT_ATTR + ']'; $.fn.fitFontSize = function (options) { return this.each(function (index, element) { this.options = $.extend({}, $.fn.fitFontSize.defaults, options); $.fn.fitFontSize.initElement(element, this.options); }); }; $.fn.fitFontSize.initElement = function (element) { var $element = $(element); var relativeToElementValue = $element.attr(RELATIVE_TO_ELEMENT_ATTR) || element.options.relativeToElement; var relativeToAttribute = $element.attr(RELATIVE_TO_ATTR_ATTR) || element.options.relativeToAttribute; var percentageValue = $element.attr(PERCENTAGE_ATTR) || element.options.percentage; element.options.scalingType = $element.attr(SCALING_TYPE_ATTR) || element.options.scalingType; var shortValues = $element.attr(INITIALIZER_ATTR); if (shortValues) { shortValues = shortValues.split(' '); $.each(shortValues, function (index, value) { if (/^[\.|#|this|parent]/.test(value)) { relativeToElementValue = value; } else if (/^[0-9]/.test(value)) { percentageValue = value; } else { relativeToAttribute = value; } }); } element.options.percentage = parseFloat(percentageValue); var $relativeToElement; switch (true) { case relativeToElementValue instanceof jQuery: $relativeToElement = relativeToElementValue; break; // Element itself case relativeToElementValue == 'parent': $relativeToElement = $element.parent(); break; // Closest element with class case /[\.|#]/.test(relativeToElementValue) == true: $relativeToElement = $element.closest(relativeToElementValue); break; // Parent element case relativeToElementValue == 'this': default: $relativeToElement = $element; } element.options.relativeToElement = $relativeToElement; $element.bind({ change: function (event, optionKey, optionValue) { if (element.options[optionKey]) { element.options[optionKey] = optionValue; } $.fn.fitFontSize.resizeElement($element, element.options); } }); // Bind the resize event of the window-tag and the element-tag itself to the resizer $(window).resize(function () { $.fn.fitFontSize.resizeElement($element, element.options); }); // Bind the the resize-function to the resizing if the user wants to manually call it $element.bind('resize', function () { $.fn.fitFontSize.resizeElement($element, element.options); }); $.fn.fitFontSize.resizeElement($element, element.options); }; $.fn.fitFontSize.resizeElement = function ($element, options) { var attributeValue = parseFloat($(options.relativeToElement).css(options.relativeToAttribute)); var newFontSize; switch(options.scalingType) { case 'font-size': default: newFontSize = attributeValue / 100 * options.percentage; break; /*case 'width': var fontSize = 1; var $scalingText = $element.find(SCALING_TEXT_SELECTOR); $element.css({ whiteSpace: 'nowrap' }); $scalingText.css({ fontSize: fontSize + 'px' }); var counter = 0; do { fontSize++; counter++; $scalingText.css({ fontSize: fontSize + 'px' }); } while($scalingText.width() / $element.width() * 100 < options.percentage && counter < 100); newFontSize = fontSize - 1; break;*/ } $element.css({fontSize: newFontSize + 'px'}); }; $.fn.fitFontSize.defaults = { relativeToElement: 'this', relativeToAttribute: 'width', percentage: '10', scalingType: 'font-size' }; $(document).ready(function () { $(INITIALIZER_SELECTOR).each(function (index, element) { $(element).fitFontSize(); }); }); $(window).load(function () { $(window).trigger('resize'); }); })(jQuery);