【发布时间】:2020-06-07 18:17:48
【问题描述】:
我注意到 Chrome 和 Safari 对元素的 position: sticky 处理略有不同。
具体来说,当粘性元素变得更高,并且当前滚动窗口以使粘性元素偏离其初始位置时,Chrome 会将 scrollTop 位置更改相同的量 - 呈现内容的外观当粘性元素在顶部生长时保持静止。
在 Safari 中,scrollTop 的位置在这种情况下保持不变,使内容看起来向下移动以适应粘性元素增加的高度。
我在下面创建了一个代码 sn-p 来演示浏览器在这种情况下的行为。上面的截图显示了这个演示在每个浏览器上的表现,但你可以在这里自己尝试一下。
function grow() {
const header = document.getElementById("header");
document.getElementById("header").classList.toggle("large-header");
updateScrollText();
}
function updateScrollText() {
const container = document.getElementById("container");
const scrollParent = getScrollParent(container);
document.getElementById("scrollbarpos1").innerHTML = scrollParent.scrollTop;
document.getElementById("scrollheight1").innerHTML = scrollParent.scrollHeight;
document.getElementById("containerheight1").innerHTML = container.offsetHeight;
document.getElementById("scrollbarpos2").innerHTML = scrollParent.scrollTop;
document.getElementById("scrollheight2").innerHTML = scrollParent.scrollHeight;
document.getElementById("containerheight2").innerHTML = container.offsetHeight;
}
function getScrollParent(node) {
if (node == null) {
return null;
}
if (node.scrollHeight > node.clientHeight) {
return node;
} else {
return getScrollParent(node.parentNode);
}
}
window.onscroll = updateScrollText;
window.onload = updateScrollText;
#header {
background-color: #CACACA;
position: sticky;
top: 0;
padding: 20px;
}
.large-header {
height: 100px;
}
.content {
background-color: #a2a6c4;
height: 1500px;
}
.shift-down {
margin-top: 50px;
}
<div id="container">
<div id="header">
<button type="button" onclick="grow()">Grow/Shrink</button>
</div>
<div class="content">
<br>
Scrollbar position: <span id="scrollbarpos1">0</span>
<br>
Scroll height: <span id="scrollheight1">0</span>
<br>
Container height: <span id="containerheight1">0</span>
<br>
<br>
Voluptatibus omnis perspiciatis consequatur magni error exercitationem saepe qui. Ipsa sint non labore voluptates. Asperiores aut non ullam aut sit omnis ducimus in. Aut enim nihil unde ad expedita. Ratione necessitatibus quasi dolorem sunt aperiam nobis ducimus.
Sequi quasi maiores eos aut non. Ipsam delectus sit facilis aut. Dolor facilis eum dignissimos. Vero reiciendis odio quis blanditiis.
Error nesciunt rem facilis. Neque labore et qui sequi eos corrupti dolorem. Reprehenderit qui voluptatem et neque ducimus ipsum similique fugit. Ea sint alias qui laborum nesciunt. Nihil ex repellendus odit sint unde fuga.
A eum nulla ut cumque necessitatibus culpa exercitationem unde. Corrupti sit minima eveniet et aut possimus sapiente. Est accusantium aut ut numquam illo.
Praesentium fugit pariatur eum ad velit distinctio culpa id. Quia voluptatum dignissimos consequatur. Eaque nihil voluptas in voluptas voluptas eius voluptas.
<br>
<br>
<div class="shift-down">
Scrollbar position: <span id="scrollbarpos2">0</span>
<br>
Scroll height: <span id="scrollheight2">0</span>
<br>
Container height: <span id="containerheight2">0</span>
</div>
</div>
</div>
我查看了W3C spec on Positioned Layout,但找不到任何具体定义它应该如何工作的东西。
所以我的问题是:
- 为什么这两种浏览器的行为不同?
- 如果有的话,哪一个是“正确的”?
- 有什么方法可以使两个浏览器的行为相同(无论哪种方式)?
【问题讨论】:
-
@RobertoVargas 也许我遗漏了一些东西,但这看起来像是一个不同的问题。它根本没有提到改变粘性元素的高度或滚动位置。
-
> 有没有办法让两个浏览器的行为相同(无论哪种方式)?嗨,克雷格,谢谢你问这个问题——我很惊讶,因为它没有出现太多。我也对解决方案很感兴趣 - 我正在考虑用 js 修补差异,但是,我不确定它会有多成功(这也意味着强大和实用)。
标签: html css css-position sticky