Last active
July 31, 2019 22:07
Revisions
-
andrewbranch revised this gist
Dec 19, 2013 . 1 changed file with 34 additions and 31 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,37 +1,40 @@ (function($) { function getTextWidth($element) { var tester = $("<div/>").text($element.text()) .css({ "position": "absolute", "float": "left", "white-space": "nowrap", "visibility": "hidden", "font": $element.css("font"), "text-transform": $element.css("text-transform"), "letter-spacing": $element.css("letter-spacing") }) .appendTo($element.parent()), width = tester.innerWidth(); tester.remove(); return width; } function AutoShrinker($element) { this.$element = $element; this.$parent = $element.parent(); this.initialFontSize = parseFloat($element.css("fontSize")); this.currentFontSize = this.initialFontSize; this.leftMarginRatio = parseFloat($element.css("marginLeft")) / this.initialFontSize; this.resize = function() { var maxWidth = this.$parent.width(), newSize = this.currentFontSize * (maxWidth / getTextWidth(this.$element)); newSize = newSize > this.initialFontSize ? this.initialFontSize : newSize; this.$element.css({ "fontSize": newSize, "marginLeft": newSize * this.leftMarginRatio }); this.currentFontSize = newSize; }; } $.fn.autoshrink = function() { return this.each(function() { var shrinker, $this = $(this); $this.data("autoshrinker", shrinker = new AutoShrinker($this)); shrinker.resize(); $(window).on("resize", function() { shrinker.resize(); }); }); }; })(jQuery); -
andrewbranch revised this gist
Oct 15, 2013 . 3 changed files with 44 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ (function($) { function getTextWidth($element) { var tester = $("<div/>").text($element.text()) .css({ "position": "absolute", "float": "left", "white-space": "nowrap", "visibility": "hidden", "font": $element.css("font") }) .appendTo(document.body), width = tester.width(); tester.remove(); return width; } function AutoShrinker($element) { this.$element = $element; this.$parent = $element.parent(); this.initialFontSize = parseFloat($element.css("fontSize")); this.currentFontSize = this.initialFontSize; this.resize = function() { var maxWidth = this.$parent.width(), newSize = this.currentFontSize * (maxWidth / getTextWidth(this.$element) - 0.1); newSize = newSize > this.initialFontSize ? this.initialFontSize : newSize; this.$element.css({ "fontSize": newSize }); this.currentFontSize = newSize; }; } $.fn.autoshrink = function() { return this.each(function() { var shrinker, $this = $(this); $this.data("autoshrinker", shrinker = new AutoShrinker($this)); shrinker.resize(); $(window).on("resize", function() { shrinker.resize(); }); }); }; })(jQuery); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ <script> $("h1").autoshrink(); </script> <div> <h1>Shrink Me</h1> </div> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,20 +0,0 @@ -
mekwall revised this gist
Oct 5, 2011 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,12 +7,13 @@ maxHeight = parent.height(), maxWidth = parent.width(), fontSize = parseInt(ourText.css("fontSize"), 10), multiplier = maxWidth/ourText.width(), newSize = (fontSize*(multiplier-0.1)); ourText.css( "fontSize", (maxFontSize > 0 && newSize > maxFontSize) ? maxFontSize : newSize ); }); }; -
mekwall revised this gist
Oct 5, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ parent = ourText.parent(), maxHeight = parent.height(), maxWidth = parent.width(), fontSize = parseInt(ourText.css("fontSize"), 10), multiplier = maxWidth/ourText.width(); ourText.css( "fontSize", -
mekwall revised this gist
Oct 5, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,5 +15,5 @@ (fontSize*(multiplier-0.1)) ); }); }; })(jQuery); -
mekwall created this gist
Oct 5, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ (function($) { $.fn.textfill = function(maxFontSize) { maxFontSize = parseInt(maxFontSize, 10); return this.each(function(){ var ourText = $("span", this), parent = ourText.parent(), maxHeight = parent.height(), maxWidth = parent.width(), fontSize = parseInt(ourText.css("font-size"), 10), multiplier = maxWidth/ourText.width(); ourText.css( "fontSize", (maxFontSize > 0 && fontSize > maxFontSize) ? maxFontSize : (fontSize*(multiplier-0.1)) ); }); } })(jQuery);