ngx-hide-on-scroll
Version:
Hide an element on scroll down or up in Angular.
16 lines (14 loc) • 4.62 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("rxjs"),require("rxjs/operators"),require("@angular/common")):"function"==typeof define&&define.amd?define("ngx-hide-on-scroll",["exports","@angular/core","rxjs","rxjs/operators","@angular/common"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["ngx-hide-on-scroll"]={},e.ng.core,e.rxjs,e.rxjs.operators,e.ng.common)}(this,(function(e,t,n,r,i){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */Object.create;function o(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,i,o=n.call(e),l=[];try{for(;(void 0===t||t-- >0)&&!(r=o.next()).done;)l.push(r.value)}catch(e){i={error:e}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(i)throw i.error}}return l}Object.create;var l,s=function(){function e(e,r,i){this.elementRef=e,this.renderer2=r,this.platformId=i,this.hideOnScroll="Down",this.classNameWhenHidden="",this.propertyUsedToHide="transform",this.valueWhenHidden="translateY(-100%)",this.valueWhenShown="translateY(0)",this.scrollingElementSelector="",this.eventElementHidden=new t.EventEmitter,this.eventElementShown=new t.EventEmitter,this.unsubscribeNotifier=new n.Subject}return e.prototype.ngAfterViewInit=function(){var e=this;if(!i.isPlatformServer(this.platformId)){var t,s;if(this.scrollingElementSelector){if(!(s=document.querySelector(this.scrollingElementSelector)))return void console.error('NgxHideOnScroll: @Input() scrollingElementSelector\nElement with selector: "'+this.scrollingElementSelector+'" not found.');t=s}else t=window,s=this.getDefaultScrollingElement();var c,u,a=n.fromEvent(t,"scroll").pipe(r.takeUntil(this.unsubscribeNotifier),r.throttleTime(50),r.map((function(){return s.scrollTop})),r.pairwise(),r.map((function(e){var t=o(e,2),n=t[0];return t[1]<n?l.Up:l.Down})),r.distinctUntilChanged(),r.share()),d=a.pipe(r.filter((function(e){return e===l.Up}))),h=a.pipe(r.filter((function(e){return e===l.Down})));"Up"===this.hideOnScroll?(c=function(){return e.hideElement()},u=function(){return e.showElement()}):(c=function(){return e.showElement()},u=function(){return e.hideElement()}),d.subscribe((function(){return c()})),h.subscribe((function(){return u()}))}},e.prototype.ngOnDestroy=function(){this.unsubscribeNotifier.next(),this.unsubscribeNotifier.complete()},e.prototype.hideElement=function(){var e=this.elementRef.nativeElement;this.classNameWhenHidden?this.renderer2.addClass(e,this.classNameWhenHidden):this.renderer2.setStyle(e,this.propertyUsedToHide,this.valueWhenHidden),this.eventElementHidden.emit()},e.prototype.showElement=function(){var e=this.elementRef.nativeElement;this.classNameWhenHidden?this.renderer2.removeClass(e,this.classNameWhenHidden):this.renderer2.setStyle(e,this.propertyUsedToHide,this.valueWhenShown),this.eventElementShown.emit()},e.prototype.getDefaultScrollingElement=function(){return document.scrollingElement||document.documentElement},e}();s.decorators=[{type:t.Directive,args:[{selector:"[ngxHideOnScroll]"}]}],s.ctorParameters=function(){return[{type:t.ElementRef},{type:t.Renderer2},{type:String,decorators:[{type:t.Inject,args:[t.PLATFORM_ID]}]}]},s.propDecorators={hideOnScroll:[{type:t.Input}],classNameWhenHidden:[{type:t.Input}],propertyUsedToHide:[{type:t.Input}],valueWhenHidden:[{type:t.Input}],valueWhenShown:[{type:t.Input}],scrollingElementSelector:[{type:t.Input}],eventElementHidden:[{type:t.Output}],eventElementShown:[{type:t.Output}]},function(e){e.Up="Up",e.Down="Down"}(l||(l={}));var c=function(){};c.decorators=[{type:t.NgModule,args:[{declarations:[s],imports:[],exports:[s]}]}],e.NgxHideOnScrollDirective=s,e.NgxHideOnScrollModule=c,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=ngx-hide-on-scroll.umd.min.js.map