Last active
May 15, 2018 09:03
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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