11/*!
2- * Scalem v1.1 .0 - A responsive text jQuery plugin
3- * Copyright 2014, Tom Doan (http://www.tohodo.com/)
4- *
5- * Scalem by Tom Doan is licensed under the MIT License.
6- * Read a copy of the license in the LICENSE file or at
7- * http://choosealicense.com/licenses/mit
8- */
2+ * Scalem v1.2 .0 - A responsive text jQuery plugin
3+ * Copyright 2014, Tom Doan (http://www.tohodo.com/)
4+ *
5+ * Scalem by Tom Doan is licensed under the MIT License.
6+ * Read a copy of the license in the LICENSE file or at
7+ * http://choosealicense.com/licenses/mit
8+ */
99
1010( function ( $ ) {
1111 $ . fn . scalem = function ( oOptions ) {
1212 var oSettings = $ . extend ( {
13- ratio : .5 , /* Scale ratio (1 = 100%) */
14- reference : null , /* Text will scale relative to this element */
15- styles : '' /* List of styles to scale (useful for buttons) */
13+ ratio : 1 , // Scale ratio (1 = 100%)
14+ reference : null , // Text will scale relative to this element
15+ styles : '' // List of styles to scale (useful for buttons)
1616 } , oOptions ) ,
1717 updateStyles = function ( o , e ) {
1818 var $o = $ ( o ) ,
1919 $oP = $o . parent ( ) ,
20- /* Create clone to get true text width */
20+ // Create clone to get true text width
2121 $o2 = $o . clone ( ) . css ( {
2222 'width' : 'auto' ,
2323 'display' : 'none' ,
2424 'white-space' : 'nowrap'
2525 } ) ,
26- /* If data attribute exists, use that instead */
27- $ref = o . getAttribute ( 'data-scale-reference' ) ? $ ( o . getAttribute ( 'data-scale-reference' ) ) : $ ( oSettings . reference ) ,
28- /* Array of styles to scale */
29- aStyles = ( o . getAttribute ( 'data-scale-styles' ) || oSettings . styles ) . split ( ' ' ) ,
30- /* Scale ratio */
31- nRatio = parseFloat ( o . getAttribute ( 'data-scale-ratio' ) ) || oSettings . ratio ,
32- /* Reference width (set to parent width by default) */
26+ // If data attribute exists, use that instead
27+ $ref = $ ( o . getAttribute ( 'data-scale-reference' ) || oSettings . reference ) ,
28+ // Array of styles to scale
29+ aStyles = ( '' + ( o . getAttribute ( 'data-scale-styles' ) || oSettings . styles ) ) . split ( ' ' ) ,
30+ // Scale ratio
31+ nRatio = Math . max ( parseFloat ( o . getAttribute ( 'data-scale-ratio' ) || oSettings . ratio ) , 0 ) ,
32+ // Reference width (set to parent width by default)
3333 nRefWidth = ( $ref . length ) ? $ref . width ( ) : $oP . width ( ) ,
3434 nTargetWidth ,
35- /* Text width */
35+ // Text width
3636 nTextWidth ;
37+ // Validate ratio
38+ if ( isNaN ( nRatio ) ) nRatio = 1 ;
3739 // Account for scrollbar?
3840 if ( $oP [ 0 ] . scrollHeight > $oP . height ( ) ) nRefWidth -= 17 ;
3941 nTargetWidth = nRefWidth * nRatio ;
4042 // Append clone to body to get inline width
4143 $o2 . appendTo ( 'body' ) ;
4244 nTextWidth = $o2 . width ( ) ;
4345 // Exit if something doesn't look right
44- if ( nTargetWidth === 0 || nTextWidth === nRefWidth ) {
46+ if ( nTargetWidth === 0 || nTextWidth === nRefWidth ) {
4547 $o2 . remove ( ) ;
4648 return ;
4749 }
4850 // Scale the text! (6px is minimum font size to get accurate ratio)
49- for ( var i = Math . round ( ( 6 / $o2 . css ( 'font-size' , '6px' ) . width ( ) ) * nTargetWidth ) , o2 = $o2 [ 0 ] ; i < nTargetWidth ; i ++ ) {
51+ for ( var i = Math . round ( ( 6 / $o2 . css ( 'font-size' , '6px' ) . width ( ) ) * nTargetWidth ) , o2 = $o2 [ 0 ] ; i < nTargetWidth ; ++ i ) {
5052 // Update font-size using native method for better performance
5153 // (see http://jsperf.com/style-vs-csstext-vs-setattribute)
5254 o2 . style . fontSize = i + 'px' ;
6163 if ( aStyles [ 0 ] ) {
6264 var nScale = $o . width ( ) / nTextWidth ,
6365 oStyles = { } ;
64- for ( var i = 0 , imax = aStyles . length ; i < imax ; i ++ ) {
66+ for ( var i = 0 , imax = aStyles . length ; i < imax ; ++ i ) {
6567 if ( ! aStyles [ i ] ) continue ;
6668 oStyles [ aStyles [ i ] ] = ( ( aStyles [ i ] === 'width' ) ? nTargetWidth : Math . round ( parseFloat ( $o . css ( aStyles [ i ] ) ) * nScale ) ) + 'px' ;
6769 }
6870 $o . css ( oStyles ) ;
6971 }
7072 } ;
7173 return this . each ( function ( ) {
72- // This scope required for resize handler
74+ // This scope is required for the resize handler
7375 var o = this ;
76+ // Set font size on load
77+ updateStyles ( o ) ;
7478 // Update CSS styles upon resize
7579 $ ( window ) . resize ( function ( e ) {
7680 updateStyles ( o , e ) ;
7781 } ) ;
78- // Set font size on load
79- updateStyles ( o ) ;
8082 } ) ;
8183 } ;
8284} ( jQuery ) ) ;
0 commit comments