UNPKG

@kddy/vue-scrollactive

Version:

Lightweight and simple to use vue component that highlights menu items as you scroll the page, also scrolling to target section when clicked.

33 lines (25 loc) 713 B
export class ScrollContainer { constructor(containerSelector) { let container = window; if (containerSelector) { container = document.querySelector(containerSelector) || window; } this.container = container; } addScrollListener(callback) { this.scrollListenerCallback = callback; this.container.addEventListener('scroll', callback); } removeScrollListener() { this.container.removeEventListener('scroll', this.scrollListenerCallback); } getDistanceFromTop() { return this.container.scrollTop || this.container.pageYOffset; } scrollTo(x, y) { return this.container.scrollTo(x, y); } getOffsetTop() { return this.container.offsetTop; } }