1/** 2 * Font Fix 3 * 4 * Modifies font-size based on element width. 5 * 6 * jQuery >= 1.11 7 * 8 * Dependencies: None 9 */ 10 11(function($) { 12 13 $.fn.fontfix = function(options) { 14 15 /* Establish defaults */ 16 var defaults = $.extend({ 17 18 max_font : 16, 19 min_font : 1, 20 threshold : 100 21 22 }, options); 23 24 /* Calculate text width */ 25 calculate_text_width = function(element) { 26 27 var text_width = 0; 28 29 $(element).find('li:visible').each(function() { 30 31 text_width += $(this).find('a').outerWidth(); 32 33 }); 34 35 return text_width; 36 37 }; 38 39 /* Change font size */ 40 change_font_size = function(element, decrease) { 41 42 var font_size = parseFloat(element.find('li').css('font-size')); 43 44 if (decrease) { 45 46 font_size = (font_size - 1 < defaults.min_font ? defaults.min_font : font_size - 1); 47 48 } else { 49 50 font_size = (font_size + 1 > defaults.max_font ? defaults.max_font : font_size + 1); 51 52 } 53 54 element.find('li').css('font-size', font_size + 'px'); 55 56 }; 57 58 /* Calculate font size */ 59 changes = function(el) { 60 61 var element = $(el); 62 63 var element_width = element.parent().width(); 64 65 var text_width = calculate_text_width(element); 66 67 /* If text is larger than element, decrease font size */ 68 if (text_width > element_width) { 69 70 while(text_width > element_width) { 71 72 change_font_size(element, true); 73 74 text_width = calculate_text_width(element); 75 76 /* If min font reached, stop updating font size */ 77 if (parseFloat(element.find('li').css('font-size')) == defaults.min_font) { 78 79 break; 80 81 } 82 83 } 84 85 } else if (text_width < element_width - defaults.threshold) { 86 87 /* If text is much smaller than element, increase font size */ 88 while(text_width < element_width) { 89 90 change_font_size(element); 91 92 text_width = calculate_text_width(element); 93 94 /* If max font reached, stop updating font size */ 95 if (parseFloat(element.find('li').css('font-size')) == defaults.max_font) { 96 97 break; 98 99 } 100 101 } 102 103 } 104 105 }; 106 107 /* Apply changes */ 108 return this.each(function() { 109 110 var container = this; 111 112 /* Apply changes on window resize */ 113 $(window).resize(function() { 114 115 if ($(this).width() >= 768) { 116 117 changes(container); 118 119 } 120 121 }); 122 123 /* Set changes on load */ 124 changes(container); 125 126 }); 127 128 }; 129 130}(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.