html - 使粘性表格标题随水平滚动移动
问题描述
我有一个具有“粘性”标题的 HTML 表格,即表格垂直滚动时,标题保持在原来的位置。
问题是我需要它与水平滚动一起移动,而不是垂直滚动。
CSS
.sticky {
position: fixed;
top: 50px;
}
JS
window.onscroll = function () { stickyFunction() };
var header = document.getElementById("theader");
var sticky = header.offsetTop;
function stickyFunction() {
if (window.pageYOffset > sticky) {
header.classList.add("sticky");
$("theader").css("left", window.pageXOffset); //this is what I'm trying to make it move with horizontal scroll
$("theader").css("margin-left", window.pageXOffset); //didn't work either
} else {
header.classList.remove("sticky");
}
}
解决方案
这是任何好奇的人的解决方案:
.sticky {
position: absolute;
}
<div id="theader">
<table id="headertable" border="0" class="corrTable">
etc...
window.onscroll = function () { stickyFunction() };
var header = document.getElementById("theader");
var sticky = header.offsetTop;
function stickyFunction() {
if (window.pageYOffset > sticky) {
header.classList.add("sticky");
$("#theader").css({ "top": ($(window).scrollTop() + 50) + "px" });
} else {
header.classList.remove("sticky");
}
}
推荐阅读
- php - 编码图像网址是可能的
- java - ScrollView 内的 LinearLayout 扩展不够远
- javascript - 根据滚动条位置更改元素的不透明度
- javascript - 我无法从 `document.getElementById` 中获取值
- python - MacOS 上的 Python3、gcc 和 clang
- reactjs - React - 如何在异步函数之外获取获取的数据?
- node.js - Postgres 连接池错误排查
- python - Python列表中第一次出现零的索引
- ruby - 如何解析列表中的两个元素以创建一个新元素
- java - 使用 Selenium 按类名查找数组中的元素