Skip to content

Instantly share code, notes, and snippets.

@leye0
Created June 28, 2016 13:56
Show Gist options
  • Save leye0/5d0c9afd0bcabdf875dcef306cde4e44 to your computer and use it in GitHub Desktop.
Save leye0/5d0c9afd0bcabdf875dcef306cde4e44 to your computer and use it in GitHub Desktop.
Aurelia Infinite Scroll custom attribute
// Usage: div is the container that wraps your items.
// <div infinite-scroll loadmore.trigger="loadMoreStuff()"></div>
import {autoinject, bindable, DOM} from 'aurelia-framework';
@autoinject()
export class InfiniteScrollCustomAttribute {
element: HTMLInputElement;
onScroll: (event: Event) => void;
constructor(element: Element) {
this.element = element as HTMLInputElement;
}
attached() {
this.onScroll = (event: Event) => {
let scrollTop = this.element.parentElement.scrollTop;
let adjustForOtherZoomLevels = 2; // When other than 100% zoomed, scrollTop is a bit inaccurate by 0 to 2 pixels.
let contentHeight = this.element.offsetHeight;
let containerHeight = this.element.parentElement.offsetHeight;
let scrollBottomThreshold = (contentHeight - containerHeight) - adjustForOtherZoomLevels;
if (scrollTop >= scrollBottomThreshold) {
this.raiseInfiniteScroll();
}
}
this.element.parentElement.addEventListener('scroll', this.onScroll);
}
detached() {
this.element.parentElement.removeEventListener('scroll', this.onScroll);
}
private raiseInfiniteScroll() {
this.element.dispatchEvent(DOM.createCustomEvent('loadmore', { bubbles: true }));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment