/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. /*
  2. jQuery Fit Text to Height plugin
  3. Version 0.1
  4. Author: Mike Brant
  5. */
  6. $.fn.fitTextToHeight = function(options) {
  7. options = $.extend({}, $.fn.fitToHeight.defaults, options);
  8. return this.each(function() {
  9. element = $(this);
  10. // we determine if this is the first time this method has been executed on this element
  11. if (element.data('initStateSet') == undefined) {
  12. // read in element's initial state into jQuery data
  13. var heightInit = element.height();
  14. element.data('heightInit', heightInit);
  15. var overflowInit = element.css('overflow');
  16. element.data('overflowInit', overflowInit);
  17. var fontSizeInit = parseInt(element.css('font-size').replace('px', ''));
  18. element.data('fontSizeInit', fontSizeInit);
  19. var topMarginInit = parseInt(element.css('margin-top').replace('px', ''));
  20. element.data('topMarginInit', topMarginInit);
  21. element.data('initStateSet', true);
  22. } else {
  23. // reset the element's css to its initial state
  24. var heightInit = element.data('heightInit');
  25. element.css('height', heightInit);
  26. var overflowInit = element.data('overflowInit');
  27. element.css('overflow', overflowInit);
  28. var fontSizeInit = element.data('fontSizeInit');
  29. element.css('font-size', fontSizeInit+'px');
  30. var topMarginInit = element.data('topMarginInit');
  31. element.data('topMarginInit', topMarginInit);
  32. }
  33. var maxScrollHeight = options.maxScrollHeight;
  34. // if the maxScrollHeight is set to zero, we try to fix text into the CSS-defined height of the element
  35. if (options.maxScrollHeight == 0) {
  36. maxScrollHeight = heightInit;
  37. }
  38. // 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.
  39. element.css('overflow', 'auto');
  40. var scrollHeightInit = element.get(0).scrollHeight;
  41. var scrollHeight = scrollHeightInit;
  42. var fontSize = fontSizeInit;
  43. // change text size to fit vertically within the defined maxScrollHeight
  44. while (scrollHeight > maxScrollHeight) {
  45. fontSize = fontSize - options.fontAdjustIncrement;
  46. element.css('font-size', fontSize+'px');
  47. scrollHeight = element.get(0).scrollHeight;
  48. }
  49. // 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.
  50. if (options.verticallyCentered == true && options.maxScrollHeight != 0 && scrollHeightInit != scrollHeight) {
  51. var parentScrollHeight = element.parent().get(0).scrollHeight;
  52. var centerInit = (parentScrollHeight - heightInit)/2;
  53. var centerNew = (parentScrollHeight - scrollHeight)/2;
  54. var topMargin = centerNew - centerInit + topMarginInit;
  55. element.css('height', scrollHeight+'px');
  56. element.css('position', 'relative');
  57. element.css('top', topMargin+'px');
  58. }
  59. // change overflow style back to original state
  60. element.css('overflow', overflowInit);
  61. })
  62. }
  63. $.fn.fitTextToHeight.defaults = {
  64. verticallyCentered: true,
  65. maxScrollHeight: 0,
  66. fontAdjustIncrement: 1
  67. }