scale fit mobile web content using viewport meta tag

前端 未结 7 1484
暗喜
暗喜 2020-11-29 17:44

I\'m trying to figure out how to leverage the mobile viewport meta tag to automatically zoom the contents of a HTML page to fit into a web view.

Constraints:

相关标签:
7条回答
  • 2020-11-29 17:59

    Adding style="width:100%;max-width:640px" to the image tag will scale it up to the viewport width, i.e. for larger windows it will look fixed width.

    0 讨论(0)
  • 2020-11-29 18:00

    For Android there is the addition of target-density tag.

    target-densitydpi=device-dpi
    

    So, the code would look like

    <meta name="viewport" content="width=device-width, target-densitydpi=device-dpi, initial-scale=0, maximum-scale=1, user-scalable=yes" />
    

    Please note, that I believe this addition is only for Android (but since you have answers, I felt this was a good extra) but this should work for most mobile devices.

    0 讨论(0)
  • 2020-11-29 18:04

    I think this should help you.

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    

    Tell me if it works.

    P/s: here is some media query for standard devices. http://css-tricks.com/snippets/css/media-queries-for-standard-devices/

    0 讨论(0)
  • 2020-11-29 18:06

    ok, here is my final solution with 100% native javascript:

    <meta id="viewport" name="viewport">
    
    <script type="text/javascript">
    //mobile viewport hack
    (function(){
    
      function apply_viewport(){
        if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)   ) {
    
          var ww = window.screen.width;
          var mw = 800; // min width of site
          var ratio =  ww / mw; //calculate ratio
          var viewport_meta_tag = document.getElementById('viewport');
          if( ww < mw){ //smaller than minimum size
            viewport_meta_tag.setAttribute('content', 'initial-scale=' + ratio + ', maximum-scale=' + ratio + ', minimum-scale=' + ratio + ', user-scalable=no, width=' + mw);
          }
          else { //regular size
            viewport_meta_tag.setAttribute('content', 'initial-scale=1.0, maximum-scale=1, minimum-scale=1.0, user-scalable=yes, width=' + ww);
          }
        }
      }
    
      //ok, i need to update viewport scale if screen dimentions changed
      window.addEventListener('resize', function(){
        apply_viewport();
      });
    
      apply_viewport();
    
    }());
    </script>
    
    0 讨论(0)
  • 2020-11-29 18:07

    In the head add this

    //Include jQuery
    <meta id="Viewport" name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
    
    <script type="text/javascript">
    $(function(){
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
      var ww = ( $(window).width() < window.screen.width ) ? $(window).width() : window.screen.width; //get proper width
      var mw = 480; // min width of site
      var ratio =  ww / mw; //calculate ratio
      if( ww < mw){ //smaller than minimum size
       $('#Viewport').attr('content', 'initial-scale=' + ratio + ', maximum-scale=' + ratio + ', minimum-scale=' + ratio + ', user-scalable=yes, width=' + ww);
      }else{ //regular size
       $('#Viewport').attr('content', 'initial-scale=1.0, maximum-scale=2, minimum-scale=1.0, user-scalable=yes, width=' + ww);
      }
    }
    });
    </script>
    
    0 讨论(0)
  • 2020-11-29 18:09

    I had same problem as yours, but my concern was list view. When i try to scroll list view fixed header also scroll little bit. Problem was list view height smaller than viewport (browser) height. You just need to reduce your viewport height lower than content tag (list view within content tag) height. Here is my meta tag;

    <meta name="viewport" content="width=device-width,height=90%,  user-scalable = no"> 
    

    Hope this will help.Thnks.

    0 讨论(0)
提交回复
热议问题