javascript - How can one select an element by type/class that’s the closest to the top of a browser window? -
i.e. if there 7 h2 headers visible in browser window. select 1 that’s closest top of window. (the first visible element). dom order doesn't matter.
i value update while scrolling. i.e. new elements become topmost (closest top of browser window) elements.
i'll lay out outline here, , i'll let fill in code:
1) check see whether each h2 element visible on screen. here's jquery plugin this, or can write own code: https://github.com/customd/jquery-visible
2) each h2 element visible on screen, check vertical offset relative browser window. 1 minimum vertical offset 1 closest top. https://api.jquery.com/offset/
3) if want update while scrolling, need create onscroll event handler , repeat steps 1 & 2 within it.
Comments
Post a Comment