I am wanting to trigger an event with vanilla javascript when an ID reaches the top of the parent div. Have seen examples of doing such with jQuery, but I don't want to load that much for a simple effect. Plus most examples deal with the top of the viewport, whereas I have a main div that is about 90% of the viewport height, with an info div above.
I am working with a long webpage, with several sections, that scrolls within the main div. When the title of each section <h2 id="title1">bla bla bla</h2>
reaches the top of the main div, I want to trigger a simple show/hide of the title <div id="info1">bla bla bla</div>
shown in the info div. The show / hide is simple enough:
function chngInfo(x) {
const targets = document.querySelectorAll('[id^="info"]');
for (let i = targets.length; i--;) {
targets[i].style.display = 'none';
}
document.getElementById('info' + x).style.display = 'block';
}
It is triggering the function that has got me stymied. One further complication is that the height of the main div varies depending on device, so can't easily do a formula based on viewport height.
It would be major bonus if a single function could look for any titleX as it passes the top while scrolling down or passes the bottom while scrolling up.
` on the page reaches the top, I want to fire the `chngInfo` function for the related infoX
– Tom Jan 19 '20 at 09:46