Dynamic viewport resizing - Phonegap ignores viewport

感情迁移 提交于 2020-01-07 04:26:08

问题


I'm using the following code to "resize my app to the screen size of the device showing it:

$(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 = 600; // min width of site
    var ratio =  ww / mw; //calculate ratio
      if( ww < mw){ //smaller than minimum size
      $('#vp').attr('content', 'initial-scale=' + ratio + '; maximum-scale=' + ratio + '; minimum-scale=' + ratio + '; user-scalable=yes; width=' + mw);
    }else{ //regular size
     $('#vp').attr('content', 'width=device-width; initial-scale=1.0');
    }
  }
});

I have a Nexus 4. If I open the website using chrome, it works just fine. However, if I use phonegap, it just ignores the viewport meta tag. (The script is working, the resulting content of the meta tag is the same as when using the browser). I see that there are a LOT of question regarding phonegap ignoring the viewport meta tag, but few of them offer any approach to solve this.


回答1:


PhoneGap uses Android WebView before KitKat (4.4), and Android WebView does not support viewport with specific width. A solution to it is to use a library that enables custom viewport support.

One choice is monaca.viewport.js which is distributed in GitHub: https://github.com/monaca/monaca.js, and here is the API reference.

This library is also a part of Monaca, a free cloud-based tool for PhoneGap development.



来源:https://stackoverflow.com/questions/21393998/dynamic-viewport-resizing-phonegap-ignores-viewport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!