jquery.fittext.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. */
  10. (function( $ ){
  11. $.fn.fitText = function( kompressor, options ) {
  12. // Setup options
  13. var compressor = kompressor || 1,
  14. settings = $.extend({
  15. 'minFontSize' : Number.NEGATIVE_INFINITY,
  16. 'maxFontSize' : Number.POSITIVE_INFINITY
  17. }, options);
  18. return this.each(function(){
  19. // Store the object
  20. var $this = $(this);
  21. // Resizer() resizes items based on the object width divided by the compressor * 10
  22. var resizer = function () {
  23. $this.css('font-size', Math.max(Math.min($this.width() / (compressor*10), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize)));
  24. };
  25. // Call once to set.
  26. resizer();
  27. // Call on resize. Opera debounces their resize by default.
  28. $(window).on('resize.fittext orientationchange.fittext', resizer);
  29. });
  30. };
  31. })( jQuery );