1/* strech jQuery Plugin 2 * 3 * Copyright (c) 2010, Tom Switzer <[email protected]> 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17(function($) { 18 19/** 20 * Expands text to fill up the entire width of its parent (or at least as much 21 * as possible). It does this in 2 ways. First, it finds the largest font-size 22 * for the text that can still fits on one line. If there is still some space 23 * left to fill, it will increase the gap between the words as well to fill the 24 * remaining space. 25 * 26 * For example, say we wanted to make the following title, "Sprockets and 27 * Widgets" take up as much of the full 400px of width as possible: 28 * 29 * <h1 style="width: 400px">Sprockets and Widgets</h1> 30 * <script> 31 * $("h1").contents().fillWidth(); 32 * </script> 33 */ 34$.fn.stretch = function(opts) { 35 opts = $.extend({}, $.fn.stretch.defaults, opts); 36 if (!(opts.max >= 0) || !(opts.min >= 0)) 37 opts.min = opts.max = 0; 38 39 this.each(function() { 40 var container = $(this), 41 contents = container.find("> .stretch--handle"); 42 43 if (!container.hasClass("stretch--resizer") || !contents) { 44 contents = $(this).wrap("<span class='stretch--handle' />").parent(); 45 container = contents.wrap("<div class='stretch--resizer' />").parent(); 46 contents.css("margin", "0").css("padding", "0"); 47 container.css("margin", "0").css("padding", "0"); 48 49 // Ensures we only work with 1 line. 50 51 container.css("white-space", "nowrap").css("overflow", "hidden"); 52 } 53 54 var idealWidth = container.width(), 55 width, 56 min = opts.min || 1, 57 max = min; 58 59 // Search for a minimum/maximum font size so we can bound our search. 60 61 if (!opts.max) { 62 do { 63 min = max; 64 max *= 2; 65 container.css("font-size", max + "px"); 66 67 // If the width isn't changing, then avoid an infinite loop. 68 var realWidth = contents.width(); 69 width = realWidth <= width ? idealWidth : realWidth; 70 71 } while (width < idealWidth); 72 } else { 73 max = opts.max; 74 if (min == max) 75 container.css("font-size", max + "px"); 76 } 77 78 if (width == idealWidth) 79 return; 80 81 // Perform a binary search to find the font-size closest to the ideal. 82 83 while (min < max) { 84 var c = Math.floor((min + max) / 2); 85 container.css("font-size", c + "px"); 86 width = contents.width(); 87 if (width == idealWidth) 88 break; 89 if (width < idealWidth) 90 min = c + 1; 91 else 92 max = c; 93 } 94 95 // We may end up on the wrong side of "perfect," so we fix this. 96 97 if (width > idealWidth) 98 container.css("font-size", (max - 1) + "px"); 99 100 // Adding a little bit of word spacing will bring us that much closer. 101 102 var spacing = 0, 103 origWidth = contents.width(), 104 maxSpacing = opts.maxSpacing; 105 do { 106 spacing += 1; 107 container.css("word-spacing", spacing + "px"); 108 width = contents.width(); 109 } while (spacing <= maxSpacing && width <= idealWidth && width > origWidth); 110 container.css("word-spacing", (spacing - 1) + "px"); 111 }); 112 113 return this; 114}; 115 116$.fn.stretch.defaults = { 117 min: 0, 118 max: 0, 119 maxSpacing: 0 120}; 121 122})(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.