用于移除移动Web应用程序中的地址栏的跨平台方法

时间:2022-02-02 12:13:00

I am working on a mobile web app and am trying to remove the address bar. Its easy enough, unless the <body>'s natural height is not tall enough to allow for scrolling. Try as I might I cannot find a reliable iphone/android, cross device method of insuring that the <body> is tall enough to allow the address bar to disappear. Many of the methods I've seen rely on screen.height which makes the page TALLER than it needs to be. It should be EXACTLY tall enough to allow the address bar to go away and no taller!

我正在开发一个移动网络应用程序,我正在尝试删除地址栏。它很容易,除非的自然高度不够高,不允许滚动。尝试我可能找不到可靠的iphone / android,交叉设备方法确保足够高以允许地址栏消失。我见过的许多方法都依赖于screen.height,这使得页面比它需要的更加严格。它应该非常高,足以让地址栏消失,不会更高!

Does anyone have a script that handles this perfectly? I all I need to to determine the height of the page minus the address bar for iphone and android.

有没有人有一个完美处理这个的脚本?我需要确定页面的高度减去iphone和android的地址栏。

I've tried:

我试过了:

screen.height //too tall
window.innerHeight //too short
document.documentElement.clientHeight //too short
document.body.clientHeight //almost but too short

JQUERY allowed.

允许JQUERY。

2 个解决方案

#1


5  

This site also has a few other suggestions, but this no-nonsense, no-worry one is available in a github:gist and answers your question (pasted here for convenience):

这个网站还有一些其他建议,但这个严肃,无忧无虑的github:gist并回答你的问题(为方便起见粘贴在这里):

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 );

As far as I can tell, the combination of extra height added to the page (which caused problems for you) and the scrollTo() statement make the address bar disappear.

据我所知,添加到页面的额外高度(导致问题)和scrollTo()语句的组合使地址栏消失。

From the same site the 'simplest' solution to hiding the address bar is using the scrollTo() method:

从同一站点,隐藏地址栏的“最简单”解决方案是使用scrollTo()方法:

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

This will hide the address bar until the user scrolls.

这将隐藏地址栏,直到用户滚动。

This site places the same method inside a timeout function (the justification is not explained, but it claims the code doesn't work well without it):

这个站点在超时函数中放置了相同的方法(没有解释理由,但它声称没有它,代码不能正常工作):

// When ready...
window.addEventListener("load",function() {
  // Set a timeout...
  setTimeout(function(){
    // Hide the address bar!
    window.scrollTo(0, 1);
  }, 0);
});

#2


1  

I think the way it works is the address bar is hidden when the page wouldn't fit. So you want a page exactly the height of the window including the address bar, i.e. window.outerHeight, no?

我认为它的工作方式是当页面不适合时隐藏地址栏。所以你想要一个页面正好是窗口的高度,包括地址栏,即window.outerHeight,不是吗?

#1


5  

This site also has a few other suggestions, but this no-nonsense, no-worry one is available in a github:gist and answers your question (pasted here for convenience):

这个网站还有一些其他建议,但这个严肃,无忧无虑的github:gist并回答你的问题(为方便起见粘贴在这里):

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 );

As far as I can tell, the combination of extra height added to the page (which caused problems for you) and the scrollTo() statement make the address bar disappear.

据我所知,添加到页面的额外高度(导致问题)和scrollTo()语句的组合使地址栏消失。

From the same site the 'simplest' solution to hiding the address bar is using the scrollTo() method:

从同一站点,隐藏地址栏的“最简单”解决方案是使用scrollTo()方法:

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

This will hide the address bar until the user scrolls.

这将隐藏地址栏,直到用户滚动。

This site places the same method inside a timeout function (the justification is not explained, but it claims the code doesn't work well without it):

这个站点在超时函数中放置了相同的方法(没有解释理由,但它声称没有它,代码不能正常工作):

// When ready...
window.addEventListener("load",function() {
  // Set a timeout...
  setTimeout(function(){
    // Hide the address bar!
    window.scrollTo(0, 1);
  }, 0);
});

#2


1  

I think the way it works is the address bar is hidden when the page wouldn't fit. So you want a page exactly the height of the window including the address bar, i.e. window.outerHeight, no?

我认为它的工作方式是当页面不适合时隐藏地址栏。所以你想要一个页面正好是窗口的高度,包括地址栏,即window.outerHeight,不是吗?