gpt4 book ai didi

javascript - 隐藏地址栏不起作用 - 需要防弹方法

转载 作者:可可西里 更新时间:2023-11-01 06:10:43 25 4
gpt4 key购买 nike

目前我正在编写某种网络应用程序,我想隐藏 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 的元素向下移动,但地址栏保持不变。

当然,我也进行了谷歌搜索并尝试了我发现的许多 fragment 。有些什么都不做,有些只是将带有 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();
})();

Source

但我对这个解决方案不是很满意,因为我不喜欢 UA 嗅探。

你有什么建议我可以尝试在没有 UA 嗅探的情况下让它工作吗?会不会是我的 HTML 导致我发布的某些脚本出现问题?

最佳答案

不知道它是否防弹,但它适用于多种设备。如果您发现警告,请告诉我。

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

关于javascript - 隐藏地址栏不起作用 - 需要防弹方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15174962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com