vvo / in-viewport

Get a callback when any element becomes visible in a viewport (body or custom viewport)
MIT License
276 stars 24 forks source link

inViewport using a callback does not work as expected? #37

Open mvirbicianskas opened 6 years ago

mvirbicianskas commented 6 years ago

is it just me and my implementation:

window.addEventListener('DOMContentLoaded', () => {
            inViewport(document.getElementById('elementID'), { offset: 100, debounce: 300 }, () => console.log('heelllo'))
        })

or did something break in the library? Because I tried logging out how many times inViewport is called and it seems it only calls twice when the event DOMContentLoaded is triggered, but shouldn't the inViewport create a bunch of events on passed-in element instances?

if used with scroll event then you loose debounce functionality because everytime onScroll happens then inViewport is always re-initialised (same like using Immediate result inViewport(elem);)

vvo commented 6 years ago

I think inViewport with callback will only be called once, you'd have to re-attach it afterwards if you want to listen continuously.

mvirbicianskas commented 6 years ago

but shouldn't the inViewport handle the scroll event whenever inViewport is passed a callback? I haven't looked into the source code, but if I remember correctly this is what I've extracted from documentation, or maybe not? 😆 Thanks anyway!

tzi commented 6 years ago

There is a watch API to reattach the watcher: https://github.com/vvo/in-viewport#callback-watcher-api