Skip to content

Instantly share code, notes, and snippets.

@jeziellopes
Last active October 19, 2022 17:31
Show Gist options
  • Save jeziellopes/1940e5c754f1c62f05cb76d852d2c4c2 to your computer and use it in GitHub Desktop.
Save jeziellopes/1940e5c754f1c62f05cb76d852d2c4c2 to your computer and use it in GitHub Desktop.
// get current element ref
const ref = useRef(null)
// persist index
let sectionIndex: number
// helper to get element index from the DOM
const getElementIndex = () => {
// if (sectionIndex) {
// console.log({ sectionIndex })
// return sectionIndex
// }
// TODO: give credits
// https://stackoverflow.com/a/11762035
// get nodes from j-scroll container
const nodes = Array.prototype.slice.call(
document.getElementById(`j-scroll-container`)?.children || {}
)
const index = nodes.indexOf(ref.current)
if (index > 0) {
sectionIndex = index
}
// return index
return sectionIndex
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment