Last active
July 31, 2019 22:07
Responds to window resize events. Max font size taken from initial font size (specify with CSS).
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 characters
(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); |
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 characters
<script> | |
$("h1").autoshrink(); | |
</script> | |
<div> | |
<h1>Shrink Me</h1> | |
</div> |
Thanks! This is exactly what I was looking for. Super simple and easy to use!
Thanks for this easy solution!
I needed a small improvement for live editing (like an live preview text from an input field). May someone need it, too :)
$.fn.autoshrink = function(eventdata) {
return this.each(function() {
var shrinker, $this = $(this);
$this.data("autoshrinker", shrinker = new AutoShrinker($this));
shrinker.resize();
$(window).on("resize", function() {
shrinker.resize();
});
if(eventdata != null && eventdata != undefined && eventdata != "null"){
$(eventdata[0]).on(eventdata[1],function(e){
shrinker.resize();
});
}
});
};
Call example: $(id+'>h4').autoshrink(['input#id','keyup']);
Sorry for not the cleanest code, isn't my main language..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey, found an issue with this. it doesn't work in Firefox because
"font": $element.css("font"),
is failing.here is a solution that seems to be working for me.
changing:
to: