Using percent for font size?

前端 未结 5 1557
庸人自扰
庸人自扰 2020-12-13 23:49

I\'ve read a fair bit about resizing fonts recently, most of it deriding using px as an unforgivable crime (ok, maybe not that bad, you get the idea) because it doesn\'t res

5条回答
  •  北海茫月
    2020-12-14 00:40

    There's a jQuery plugin called FitText. It resizes text based on percents. If the visitor for some reason has JavaScript disabled, it'll just display as normal, so set a reasonable PX or EM backup.

    It depends on jQuery, so you'll need to include that in your page (if you don't have it already).


    jquery.fittext.js for reference:

    /*global jQuery */
    /*! 
    * FitText.js 1.0
    *
    * Copyright 2011, Dave Rupert http://daverupert.com
    * Released under the WTFPL license 
    * http://sam.zoy.org/wtfpl/
    *
    * Date: Thu May 05 14:23:00 2011 -0600
    */
    
    (function( $ ){
    
        $.fn.fitText = function( kompressor, options ) {
    
            var settings = {
            'minFontSize' : Number.NEGATIVE_INFINITY,
            'maxFontSize' : Number.POSITIVE_INFINITY
          };
    
                return this.each(function(){
                    var $this = $(this);              // store the object
                    var compressor = kompressor || 1; // set the compressor
    
            if ( options ) { 
              $.extend( settings, options );
            }
    
            // Resizer() resizes items based on the object width divided by the compressor * 10
                    var resizer = function () {
                        $this.css('font-size', Math.max(Math.min($this.width() / (compressor*10), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize)));
                    };
    
                    // Call once to set.
                    resizer();
    
                    // Call on resize. Opera debounces their resize by default. 
            $(window).resize(resizer);
    
                });
    
        };
    
    })( jQuery );
    

提交回复
热议问题