目前我正在編寫某種網絡應用程序,我想隱藏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
向下移動,但地址欄保持不變。
當然,我也做了谷歌搜索,並嘗試了很多我發現的片段。有些什麼都沒做,有的只是將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導致我發佈的一些腳本的問題?
嘿,感謝你的答案 - 良藥苦口,呵呵;-)不幸的是,我還沒有找到一個真的* *滿足此解決方案。目前最大的警告是iOS 7,我認爲,如果我沒有弄錯地址欄,就不可能再隱藏地址欄,這會使任何解決方案都無用。 – Sven 2013-10-18 18:02:08