How do I increase the font size based on the window width?

后端 未结 8 1354
一向
一向 2020-12-05 08:26

I am creating a web-based app that will be displayed on television and other large monitors. I am wanting a quick and easy way to increase the font size to fit the window si

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 08:59

    Beejamin's answer worked as expected, I added a set timeout so it would scale in real time. It doesn't reverse scale though.

    $(document).ready(function() {scaleFont();});
    
        function scaleFont() {
    
          var viewPortWidth = $(window).width();
    
          if (viewPortWidth > 1900) {$('body').addClass('extraWide')}
          else if (viewPortWidth > 1400) {$('body').addClass('wide')}
          else if (viewPortWidth > 1000) {$('body').addClass('standard')}
          else if (viewPortWidth > 700) {$('body').addClass('narrow')}
          else {$('body').addClass('extraNarrow')}
    
          setTimeout(scaleFont, 100); 
        }
    

    EDIT: SOLVED REVERSE EFFECT

    $(document).ready(function() {scaleFont();});
    
        function scaleFont() {
    
          var viewPortWidth = $(window).width();
    
          if (viewPortWidth >= 1900) {$('body').addClass('extraWide').removeClass('wide, standard, narrow, extraNarrow')}
          else if (viewPortWidth >= 1400) {$('body').addClass('wide').removeClass('extraWide, standard, narrow, extraNarrow')}
          else if (viewPortWidth >= 1000) {$('body').addClass('standard').removeClass('extraWide, wide, narrow, extraNarrow')}
          else if (viewPortWidth >= 700) {$('body').addClass('narrow').removeClass('extraWide, standard, wide, extraNarrow')}
          else {$('body').addClass('extraNarrow').removeClass('extraWide, standard, wide, narrow')}
    
    
          setTimeout(scaleFont, 100); 
        }
    

提交回复
热议问题