jquery.fittext.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*global jQuery */
  2. /*!
  3. * FitText.js 1.2
  4. *
  5. * Copyright 2011, Dave Rupert http://daverupert.com
  6. * Released under the WTFPL license
  7. * http://sam.zoy.org/wtfpl/
  8. *
  9. * Date: Thu May 05 14:23:00 2011 -0600
  10. */
  11. (function( $ ){
  12. $.fn.fitText = function( kompressor, options ) {
  13. // Setup options
  14. var compressor = kompressor || 1,
  15. settings = $.extend({
  16. 'minFontSize' : Number.NEGATIVE_INFINITY,
  17. 'maxFontSize' : Number.POSITIVE_INFINITY
  18. }, options);
  19. return this.each(function(){
  20. // Store the object
  21. var $this = $(this);
  22. // Resizer() resizes items based on the object width divided by the compressor * 10
  23. var resizer = function () {
  24. $this.css('font-size', Math.max(Math.min($this.width() / (compressor*10), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize)));
  25. };
  26. // Call once to set.
  27. resizer();
  28. // Call on resize. Opera debounces their resize by default.
  29. $(window).on('resize.fittext orientationchange.fittext', resizer);
  30. });
  31. };
  32. })( jQuery );