PageRenderTime 31ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/jquery.fitTextToHeight.js

http://jquery-fit-text-to-height.googlecode.com/
JavaScript | 75 lines | 54 code | 8 blank | 13 comment | 12 complexity | 7dd812fa8f13df197c0daaf9a0c089b7 MD5 | raw file
 1/*
 2jQuery Fit Text to Height plugin
 3Version 0.1
 4Author: Mike Brant
 5*/
 6$.fn.fitTextToHeight = function(options) {
 7	options = $.extend({}, $.fn.fitToHeight.defaults, options);
 8	
 9	return this.each(function() {
10		element = $(this);
11		
12		// we determine if this is the first time this method has been executed on this element
13		if (element.data('initStateSet') == undefined) {
14			// read in element's initial state into jQuery data
15			var heightInit = element.height();
16			element.data('heightInit', heightInit);
17			var overflowInit = element.css('overflow');
18			element.data('overflowInit', overflowInit);
19			var fontSizeInit = parseInt(element.css('font-size').replace('px', ''));
20			element.data('fontSizeInit', fontSizeInit);
21			var topMarginInit = parseInt(element.css('margin-top').replace('px', ''));
22			element.data('topMarginInit', topMarginInit);
23			element.data('initStateSet', true);
24		} else {
25			// reset the element's css to its initial state
26			var heightInit = element.data('heightInit');
27			element.css('height', heightInit);
28			var overflowInit = element.data('overflowInit');
29			element.css('overflow', overflowInit);			
30			var fontSizeInit = element.data('fontSizeInit');
31			element.css('font-size', fontSizeInit+'px');
32			var topMarginInit = element.data('topMarginInit');
33			element.data('topMarginInit', topMarginInit);
34		}
35
36		var maxScrollHeight = options.maxScrollHeight;	
37		// if the maxScrollHeight is set to zero, we try to fix text into the CSS-defined height of the element
38		if (options.maxScrollHeight == 0) {
39			maxScrollHeight = heightInit;
40		}
41		
42		// set overflow to auto. This is needed in order to be able to calculate the scrollHeight correctly (if overflow is set to hidden this will not work). We will set the element overflow back to initial value once resize is complete.
43		element.css('overflow', 'auto');
44
45		var scrollHeightInit = element.get(0).scrollHeight;
46		var scrollHeight = scrollHeightInit;
47		var fontSize = fontSizeInit;
48		
49		// change text size to fit vertically within the defined maxScrollHeight
50		while (scrollHeight > maxScrollHeight) {
51			fontSize = fontSize - options.fontAdjustIncrement;
52			element.css('font-size', fontSize+'px');
53			scrollHeight = element.get(0).scrollHeight;
54		}
55		
56		// if text is to be vertically centered and the text is not set to fit to the initial CSS-defined height, we need to shift the location of the element to make it appear verticaly centered. We do this be modifying the top-margin css property.
57		if (options.verticallyCentered == true && options.maxScrollHeight != 0 && scrollHeightInit != scrollHeight) {
58			var parentScrollHeight = element.parent().get(0).scrollHeight;
59			var centerInit = (parentScrollHeight - heightInit)/2;
60			var centerNew = (parentScrollHeight - scrollHeight)/2;
61			var topMargin = centerNew - centerInit + topMarginInit;
62			element.css('height', scrollHeight+'px');
63			element.css('position', 'relative');
64			element.css('top', topMargin+'px'); 
65		}
66		
67		// change overflow style back to original state
68		element.css('overflow', overflowInit);
69	})
70}
71$.fn.fitTextToHeight.defaults = {
72	verticallyCentered: true,
73	maxScrollHeight: 0,
74	fontAdjustIncrement: 1
75}