radiodraws
9/26/2017 - 11:07 AM

font responsive #javascript

font responsive #javascript

  $(window).load(function() {
    //----ADJUST FONTSIZE IN MOBILE
    if (isMobile()) {
      $('h1, h2, h3, h4').each(function() {


        var $this = $(this);
        var text = $this.text();
        $this.empty().append('<span style="color:inherit">' + text + '</span>');
        var width = $this.width();
        var actfont = parseInt($this.css('font-size').replace('px', ''));
        var $spanText = $this.find('span');


        for (var i = 1; i < actfont; i++) {
          var scrollw = $spanText.width();

          if (scrollw > width) {

            console.log('scrollw: ' + scrollw + ' width: ' + $spanText.width());
            $spanText.css({
              'font-size': (actfont - i) + 'px'
            });
          } else {
            break;
          }
        }


      });
    }
  });