Home:ALL Converter>How to use Scrollspy & Affix in Angular 2

How to use Scrollspy & Affix in Angular 2

Ask Time:2016-09-12T21:54:50         Author:Gerald Hughes

Json Formatter

I noticed that you cannot use in angular 2 components bootstrap feature like data-spy="affix"

Does anyone know how to use affix and scrollspy in angular 2? (Example)

Author:Gerald Hughes,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39452007/how-to-use-scrollspy-affix-in-angular-2
Antara Datta :

You could write your small directive which would do the same thing. I'm sharing my code:\n\nDirective:\n\n@Directive({\n selector: '[scrollPoint]'\n})\nexport class ScrollPointDirective {\n @Input() scrollPoint: number;\n constructor(\n @Inject(DOCUMENT) private document: Document,\n private renderer: Renderer,\n private el: ElementRef\n ) { }\n\n @HostListener('window:scroll', [])\n onWindowScroll() {\n const windowScroll = this.document.body.scrollTop;\n if (windowScroll > this.scrollPoint) {\n //add class to the native element\n this.renderer.setElementClass(this.el.nativeElement, 'sticky-nav', true);\n } else {\n //remove class from native element\n this.renderer.setElementClass(this.el.nativeElement, 'sticky-nav', false);\n }\n\n }\n\n }\n\nThe @Input scrollPoint parameter can be passed from your HTML\n\nHTML:\n\n <div [scrollPoint]="235">\n <ul class="nav-stacked">\n //your code\n </ul>\n </div>\n\nAdd CSS to the class added in the directive:\n\nCSS\n\n.sticky-nav {\n position: sticky;\n top: 90px;\n bottom: 0;\n }\n\nI haven't been able to integrate affix to angular2 as well, but this did it for me.\nHope it helps.",
2017-07-12T04:04:07
yy