[英]How to fix/stick an header row on scroll?
請在問題中添加您的代碼,不要將鏈接粘貼到屏幕截圖中。 如果您還添加 css 代碼,這將有助於獲得更好更快的答案,因為樣式是在樣式表中完成的。
但我想我有你的問題。
如果您的 header 位於頁面頂部,則它是一個簡單的 css 屬性,您將其添加到樣式表中的 class 中:
.riga_sup {
position: fixed;
top: 0;
}
看一下例子: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_fixed_menu
If your header is not at the top of your page, but you want it to be fixed if it is at the top position after scrolling, you will need a little bit of javascript to add the class with the "position: fixed;" 滾動到達元素時的樣式。 在示例中,header 的 ID 為“myHeader”。
<script>
window.onscroll = function() {myFunction()};
var header = document.getElementById("header");
var sticky = header.offsetTop;
function myFunction() {
if (window.pageYOffset > sticky) {
header.classList.add("sticky");
} else {
header.classList.remove("sticky");
}
}
</script>
在您的 CSS 中,您將屬性賦予“粘性”class:
.sticky {
position: fixed;
top: 0;
}
這是一個例子: https://www.w3schools.com/howto/howto_js_sticky_header.asp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.