Skip to content

Instantly share code, notes, and snippets.

@Shuumatsu
Created November 7, 2017 02:58
Show Gist options
  • Save Shuumatsu/80d1d331c3e80c9d2f37a1a698ecc263 to your computer and use it in GitHub Desktop.
Save Shuumatsu/80d1d331c3e80c9d2f37a1a698ecc263 to your computer and use it in GitHub Desktop.
export const eventName = 'in view'
export const tagName = 'inview-observer'
const io = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
const target = entry.target
target.dispatchEvent(new Event('in view'))
})
}, { threshold: 0.25 })
class InViewObserver extends HTMLElement {
connectedCallback() {
io.observe(this)
}
disconnectedCallback() {
io.unobserve(this)
}
}
customElements.define(
tagName,
InViewObserver,
{ extends: 'div' }
)
export default InViewObserver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment