Skip to content

Instantly share code, notes, and snippets.

@Rwin90
Last active May 15, 2018 09:03
Show Gist options
  • Save Rwin90/34f7b8a1f6bdef91f613ab3889e01846 to your computer and use it in GitHub Desktop.
Save Rwin90/34f7b8a1f6bdef91f613ab3889e01846 to your computer and use it in GitHub Desktop.
its an angular 4+ directive that prevent parent element scroll after child element scroll rich to end
import {Directive, ElementRef, HostListener} from '@angular/core';
@Directive({
selector: '[appMyScroll]'
})
export class ItsMyScrollDirective {
constructor(private _elementRef: ElementRef ) {
}
@HostListener('mousewheel' , [ '$event' ]) onScroll( e) {
const d = e.wheelDelta;
const h = this._elementRef.nativeElement.offsetHeight;
const sh = this._elementRef.nativeElement.scrollHeight;
const st = this._elementRef.nativeElement.scrollTop;
if((st >= (sh - h) && d < 0) || (st === 0 && d > 0)) {
e.preventDefault();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment