/*! font-size adapter jquery plugin - v0.1.0 * https://github.com/matijamrkaic/font-size-adapter * Copyright (c) 2017 Matija Mrkaić; Licenced MIT */ (function ($){ "use strict"; $.fn.fontSizeAdapter = function( options ){ var $el = this; // Default Settings var settings = $.extend({ fontMax: 0, // Set maximum font-size limit fontMin: 0, // Set minimum font-size limit enlarge: true, // Enable font-size to be increased from it's original size onResize: true // Enable automatic recalculation on $(window).resize(); }, options); var fontOriginal; var fontAdapted; var biggest = {}; // Calculate values and find biggest item (relative to available space) function setup(){ // reset data fontOriginal = $el.css('font-size','').css('font-size'); fontOriginal = parseInt(fontOriginal, 10); biggest.coefficient = settings.enlarge ? 999 : 1; // Find biggest item in selection, relatively to it's parent /*$el.each(function(){ // pay attention to margins/paddings of elements if($(this).closest('a').is(":visible")){ var width = Math.ceil($(this).outerWidth(true)); // console.info($(this).parent().outerWidth()); var parentWidth = Math.ceil($(this).parent().width()+5); var coefficient = parentWidth / width; if( coefficient < biggest.coefficient ){ biggest = {el: this, coefficient: coefficient}; } console.log(fontOriginal); console.log(width); console.log(parentWidth); console.log(coefficient); } });*/ // pay attention to margins/paddings of elements if($el.closest('a').is(":visible")){ var width = Math.ceil($el.outerWidth(true)); // console.info($(this).parent().outerWidth()); var parentWidth = Math.ceil($el.parent().width()+5); var coefficient = parentWidth / width; if( coefficient < biggest.coefficient ){ biggest = {el: $el, coefficient: coefficient}; } _apply(); } } // Apply to all elements function _apply(){ if(biggest.coefficient < 1){ fontAdapted = Math.floor( biggest.coefficient * fontOriginal ); if( settings.fontMax ){ fontAdapted = Math.min( fontAdapted, parseInt(settings.fontMax) ); } if( settings.fontMin ){ fontAdapted = Math.max( fontAdapted, parseInt(settings.fontMin) ); } if( fontAdapted != fontOriginal ){ $el.css('font-size', fontAdapted); } // Add 'finished' class to elements $el.addClass('font-size-adapted'); }else{ $el.css('font-size', fontOriginal); // Add 'finished' class to elements $el.addClass('font-size-adapted'); } } // Start setup(); // If $(window).resize watch enabled // if( settings.onResize ){ // $(window).resize(function (e) { // e.preventDefault(); // setup(); // }) // } // Enable chaining and finish return this; }; }(jQuery));