我知道 Web 开发人员现在可以在没有 js 的情况下完成这样的事情真是太好了:
.sticky {
position: -webkit-sticky;
position: -moz-sticky;
position: -ms-sticky;
position: -o-sticky;
top: 15px;
}
对比
<style>
.sticky {
position: fixed;
top: 0;
}
.header {
width: 100%;
background: #F6D565;
padding: 25px 0;
}
</style>
<div class="header"></div>
<script>
var header = document.querySelector('.header');
var origOffsetY = header.offsetTop;
function onScroll(e) {
window.scrollY >= origOffsetY ? header.classList.add('sticky') :
header.classList.remove('sticky');
}
document.addEventListener('scroll', onScroll);
</script>
但是在实际浏览器的引擎盖下,它不是在执行相同类型的渲染,并占用相同数量的内存。本质上,浏览器中是否存在渲染 CSS 的较低级别的代码找到位置:-webkit-sticky,并且与上面的 javascript 渲染有些相同?