gpt4 book ai didi

html - CSS 背景在 iOS 中拉伸(stretch)以填充高度但滚动上有空白

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:51:57 25 4
gpt4 key购买 nike

此 CSS 让我的背景在 iOS 中填充 100% 的屏幕高度,但有一个小问题 - 当您向下滚动时,最初会有空白,然后当您松开手指并停止滚动时,背景图像“调整”并再次填充 100% 的屏幕高度。如果继续滚动,问题不会在同一页面上再次出现,只是第一次出现。有人可以帮忙吗?谢谢

#background {
background: url(../img/background.jpg) center repeat-x;
position: fixed;
background-size: auto 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: -1
}

最佳答案

发生这种情况是因为您的 #background 元素设置为“跟随”窗口的高度:

#background {
...
position: fixed;
top: 0;
bottom: 0;
...
}

但是,当您第一次将内容从 460px 向下滚动到 529px 时,Safari 中的 window 会更改其大小:

enter image description here enter image description here enter image description here

从这些屏幕截图中可以看出,加载网页时,window.innerHeight 等于 460px(在 iPhone5 上)。然后,当您在手指触摸屏幕时开始向下滚动页面时,window.innerHeight 增加到 529px 但内容尚未更新。只有当手指从屏幕上松开时,#background 元素才会更新为等于 529px 的新窗口高度。

要解决此问题,您应该确保 #background 元素比初始窗口高度高 529-460 = 69px。或者 #background 元素的高度恒定为 529px(或更高):

#background {
...
position: fixed;
top: 0;
bottom: -69px;
...
}

#background {
...
position: fixed;
top: 0;
/* use height instead of bottom */
height: 529px;
...
}

这是解决此问题的最终代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Fixed background</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="css/styles.css?v=1.0">
<style>
#background {
background: url(bkgd.png) repeat;
position: fixed;
background-size: 100% auto;
top: 0;
left: 0;
right: 0;
bottom: -69px;
z-index: -1;
}
</style>
</head>
<body>
<div id="background"></div>
<div style="height: 5000px;"></div>
</body>
</html>

关于html - CSS 背景在 iOS 中拉伸(stretch)以填充高度但滚动上有空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22741143/

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