Created
November 24, 2021 12:39
-
-
Save sagarpanchal/969eb1ab00621ddf7ad2191e00f6ad9b to your computer and use it in GitHub Desktop.
Stick Table Columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const stickyIndex = 5; | |
const rows = document.querySelectorAll("table tr"); | |
const cellSlice = Array.prototype.slice.call(rows[0].children, 0, stickyIndex); | |
const widthList = Array.prototype.map.call(cellSlice, (cell) => cell.offsetWidth); | |
const leftOffsetList = widthList.map((_, index) => { | |
const output = -10; | |
for (let i = 0; i < index; i++) output = output + (widthList[i] - 1); | |
return output; | |
}); | |
Array.prototype.forEach.call(rows, (row) => { | |
const cellList = Array.prototype.slice.call(row.children, 0, stickyIndex); | |
Array.prototype.forEach.call(cellList, (cell, index) => { | |
cell.style.position = "sticky"; | |
cell.style.left = `${leftOffsetList[index]}px`; | |
cell.style["z-index"] = (cell.style["z-index"] ?? 0) + 1000; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment