将FitText与change()方法一起使用

时间:2015-02-20 03:49:26

标签: javascript jquery html html5 fittextjs

如何触发以下fittext.js函数

$.fn.fitText = function( kompressor, options ) {
    var compressor = kompressor || 1,
            settings = $.extend({
              'minFontSize' : Number.NEGATIVE_INFINITY,
              'maxFontSize' : Number.POSITIVE_INFINITY
            }, options);

        return this.each(function(){

          // Store the object
          var $this = $(this);

          // 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)));
          };
    )};

使用change()方法?

$('#drop').change(function(){
     $('#tf').css('width', $(this).val());
     //resizer(); // does not work
     //$('#tf').fitText(1.2, { minFontSize: '20px', maxFontSize: '40px' });// does not work
});

1 个答案:

答案 0 :(得分:0)

您需要触发调整大小事件,因为fittext插件会侦听window的resize事件以调用内部resize方法

$(window).on('resize.fittext orientationchange.fittext', resizer);

所以

$('#tf').fitText(1.2, {
    minFontSize: '20px',
    maxFontSize: '40px'
});

$('#drop').change(function () {
    $('#tf').css('width', $(this).val());
    $(window).trigger('resize.fittext');
});

演示:Fiddle - 将宽度设置为100/300/500