Skip to content

Additional options and optional compressor argument #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 50 additions & 58 deletions example.html
Original file line number Diff line number Diff line change
@@ -1,60 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>FitText</title>

<style type="text/css">
body {
background: #233a40;
color: #fff;
font: 16px/1.8 sans-serif;
}
.container {
width: 84%;
margin:0 auto;
max-width:1140px;
}
header {
width: 100%;
margin:0px auto;
}
h1 {
background: rgba(0,0,0,0.2);
text-align: center;
color:#fff;
font: 95px/1 "Impact";
text-transform: uppercase;
display: block;
text-shadow:#253e45 -1px 1px 0,
#253e45 -2px 2px 0,
#d45848 -3px 3px 0,
#d45848 -4px 4px 0;
margin: 5% auto 5%;
}
</style>

<!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
</head>

<body>

<div class="container">
<header>
<h1 id="fittext1">Squeeze with FitText</h1>
<h1 id="fittext2">Squeeze with FitText</h1>
<h1 id="fittext3">Squeeze with FitText</h1>
</header>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="jquery.fittext.js"></script>
<script type="text/javascript">
$("#fittext1").fitText();
$("#fittext2").fitText(1.2);
$("#fittext3").fitText(1.1, { minFontSize: '50px', maxFontSize: '75px' });
</script>

</body>
<html>
<head>
<meta charset="utf-8" />
<title>jQuery.fitText Additional Options</title>
<style>
html {
font-family:sans-serif;
}

h1 {
text-transform:uppercase;
font-size:1em;
}

span {
cursor:pointer;
}
</style>
<script src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script src="jquery.fittext.js"></script>
<script>
//You should never run `fitText()` before the page is fully rendered
//(i.e. `window.onload`) because `width()` may not be work properly.
$(window).bind("load", function(){
var q_updateSize=true;

$("h1").fitText({
"compressor":1.97,
"proxy":$("body"),
"before":function(size){
console.log(size, this);

return q_updateSize;
},
"after":function(size){
console.log(size, this);
}
});

$("span").bind("click", function(){
q_updateSize=!q_updateSize;
});
});
</script>
</head>
<body>
<h1>jQuery.fitText Additional Options</h1>
<p><span>Click to toggle FitText</span></p>
<p>101</p>
</body>
</html>
83 changes: 54 additions & 29 deletions jquery.fittext.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,60 @@
* Date: Thu May 05 14:23:00 2011 -0600
*/

(function( $ ){
(function($){
//jquery fitText([number compressor,] object options)
$.fn.fitText=function(compressor, options){
options=options || {};

if(compressor){
if(typeof compressor=="object"){
//If the options object was specified using the first arguments, the second argument is ignored.
options=compressor;
}else{
//If the compressor was specified using the first arguments, that value is used, regardless of the options object.
options.compressor=compressor;
}
}

//Setup options
var settings=$.extend({
//Number
"compressor":1,
//Must be parseable by `parseFloat()`
"minFontSize":Number.NEGATIVE_INFINITY,
//Must be parseable by `parseFloat()`
"maxFontSize":Number.POSITIVE_INFINITY,
//A function called before the new font-size is applied. The font-size
//is passed as first argument and `this` belongs to the DOM element.
//If `false` (checked using `===`) is returned, the font-size won't be applied.
"before":$.noop,
//A function called after the new font-size was applied. The font-size
//is passed as first argument and `this` belongs to the DOM element.
"after":$.noop,
//A jQuery object used instead of the object this function was called on.
//Does not affect `this` in "before" and "after".
"proxy":null
}, options);

return this.each(function(){
//Store the object
var $this=$(this);

$.fn.fitText = function( kompressor, options ) {
//Resizer() resizes items based on the object width divided by the compressor * 10
var resizer=function(){
var size=Math.max(Math.min($this.width()/(settings.compressor*10), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize));

if(settings.before.apply($this.get(0), [size])!==false){
(settings.proxy || $this).css("font-size", size);
}
settings.after.apply($this.get(0), [size]);
};

// Setup options
var compressor = kompressor || 1,
settings = $.extend({
'minFontSize' : Number.NEGATIVE_INFINITY,
'maxFontSize' : Number.POSITIVE_INFINITY
}, options);
//Call once to set.
resizer();

return this.each(function(){

// Store the object
var $this = $(this);

// Resizer() resizes items based on the object width divided by the compressor * 10
var resizer = function () {
$this.css('font-size', Math.max(Math.min($this.width() / (compressor*10), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize)));
};

// Call once to set.
resizer();

// Call on resize. Opera debounces their resize by default.
$(window).on('resize.fittext orientationchange.fittext', resizer);

});

};

})( jQuery );
//Call on resize. Opera debounces their resize by default.
$(window).on("resize.fittext orientationchange.fittext", resizer);
});
};
})(jQuery);