4

現時点では、ある種の Web アプリを作成しており、iOS デバイス、できれば Android デバイスでもアドレス バーを非表示にしたいと考えています。
通常、私はこれを行います

window.addEventListener( 'load', function () {
  setTimeout( function () {
    window.scrollTo( 0, 1 );
  }, 0 );
});

しかし、ページにスクロールするのに十分なコンテンツがないため、これは今は機能しません。
これは一般的な問題であり、複数の解決策があることはわかっていますが、小規模で防弾の解決策が望ましいと思います。
実際、この質問を見つけたときはとても嬉しかったです:http://stackoverflow.com/questions/9678194/cross-platform-method-for-removing-the-address-bar-in-a-mobile-web-app

このコードが投稿された場​​所:

function hideAddressBar()
{
  if(!window.location.hash)
  {
      if(document.height < window.outerHeight)
      {
          document.body.style.height = (window.outerHeight + 50) + 'px';
      }

      setTimeout( function(){ window.scrollTo(0, 1); }, 50 );
  }
}

window.addEventListener("load", function(){ if(!window.pageYOffset){ hideAddressBar(); } } );
window.addEventListener("orientationchange", hideAddressBar );

残念ながら、これは私にはうまくいきません。padding-topパーセンテージで設定され た一部の要素が下に移動するため、何かが発生することがわかりますが、アドレスバーは残ります。

もちろん、私も Google 検索を行い、見つけた多くのスニペットを試しました。何もしないものもあれば、要素をpadding-top少し下に移動しただけのものもあります。
私が見つけた唯一の作業コードはこれです:

var page = document.getElementById('page'),
    ua = navigator.userAgent,
    iphone = ~ua.indexOf('iPhone') || ~ua.indexOf('iPod'),
    ipad = ~ua.indexOf('iPad'),
    ios = iphone || ipad,
    // Detect if this is running as a fullscreen app from the homescreen
    fullscreen = window.navigator.standalone,
    android = ~ua.indexOf('Android'),
    lastWidth = 0;

if (android) {
  // Android's browser adds the scroll position to the innerHeight, just to
  // make this really difficult. Thus, once we are scrolled, the
  // page height value needs to be corrected in case the page is loaded
  // when already scrolled down. The pageYOffset is of no use, since it always
  // returns 0 while the address bar is displayed.
  window.onscroll = function() {
    page.style.height = window.innerHeight + 'px'
  } 
}
var setupScroll = window.onload = function() {
  // Start out by adding the height of the location bar to the width, so that
  // we can scroll past it
  if (ios) {
    // iOS reliably returns the innerWindow size for documentElement.clientHeight
    // but window.innerHeight is sometimes the wrong value after rotating
    // the orientation
    var height = document.documentElement.clientHeight;
    // Only add extra padding to the height on iphone / ipod, since the ipad
    // browser doesn't scroll off the location bar.
    if (iphone && !fullscreen) height += 60;
    page.style.height = height + 'px';
  } else if (android) {
    // The stock Android browser has a location bar height of 56 pixels, but
    // this very likely could be broken in other Android browsers.
    page.style.height = (window.innerHeight + 56) + 'px'
  }
  // Scroll after a timeout, since iOS will scroll to the top of the page
  // after it fires the onload event
  setTimeout(scrollTo, 0, 0, 1);
};
(window.onresize = function() {
  var pageWidth = page.offsetWidth;
  // Android doesn't support orientation change, so check for when the width
  // changes to figure out when the orientation changes
  if (lastWidth == pageWidth) return;
  lastWidth = pageWidth;
  setupScroll();
})();

ソース

しかし、私はUAスニッフィングの友人ではないので、このソリューションにはあまり満足していません.

UA のスニッフィングなしで機能させるために私ができることについて何か提案はありますか? 私が投稿した一部のスクリプトで問題を引き起こすのは、私の HTML でしょうか?

4

1 に答える 1

1

防弾かどうかはわかりませんが、多くのデバイスで動作します。警告を見つけた場合は、お知らせください。

if (((/iphone/gi).test(navigator.userAgent) || (/ipod/gi).test(navigator.userAgent)) &&
    (!("standalone" in window.navigator) && !window.navigator.standalone)) {
    offset = 60;
    $('body').css('min-height', (window.innerHeight + offset) + 'px');
    setTimeout( function(){ window.scrollTo(0, 1); }, 1 );
}

if ((/android/gi).test(navigator.userAgent)) {
    offset = 56;
    $('html').css('min-height', (window.innerHeight + offset) + 'px');
    setTimeout( function(){ window.scrollTo(0, 1); }, 0 );
}
于 2013-10-18T17:58:06.923 に答える