JonnyBGod / ngx-scrollspy

Angular ScrollSpy Service
MIT License
94 stars 21 forks source link

Does Spy any element scroll can do this ? #170

Open rainstormza opened 7 years ago

rainstormza commented 7 years ago

1492740850034

I want to active css class when I scroll to div id that I define.

thanks

JonnyBGod commented 7 years ago

Yes this should be easy to achieve. Try something like:

@Component({
  template: `
<div scrollSpy [scrollSpyIndex]="{id: 'contexts', selector: 'context'}">
  <div class="context" *ngFor="let context of (contexts | async); let i = index"  id="context_{{i}}">
    ...
  </div>
</div>
  `,
  providers: [
   { provide: 'windowObject', useValue: window }
  ],
})
export class MyComponent {
  public selectContext$: ReplaySubject<string> = new ReplaySubject(1);

  constructor(
    @Inject('windowObject') private win: Window,
    private scrollSpyService: ScrollSpyService,
    private scrollSpyIndex: ScrollSpyIndexService
  ) {
    this.scrollSpyService.getObservable('window')
      .subscribe((e) => {
        let currentScrollPosition: int;
        if (typeof e.target.scrollingElement !== 'undefined') {
          currentScrollPosition = e.target.scrollingElement.scrollTop;
        } else if (typeof e.target.scrollY !== 'undefined') {
          currentScrollPosition = e.target.scrollY;
        } else if (typeof e.target.pageYOffset !== 'undefined') {
          currentScrollPosition = e.target.pageYOffset;
        }
        let items: any[] = this.scrollSpyIndex.getIndex('contexts');

        if (!items || !items.length) {
          return;
        }

        let contextId: string;
        for (let i = items.length - 1; i >= 0; i--) {
          if (this.currentScrollPosition - items[i].offsetTop >= 0) {
             contextId = items[i].id;
             break;
          }
        }

        this.selectContext$.next(contextId);
      });
  }
}

You can then subscribe to selectContext$.

rainstormza commented 7 years ago

@JonnyBGod excellent , thanks I gonna try this anyway , I think that if you can provide this thing to demo or tutorial step by step , it pretty easy to use 👍

JonnyBGod commented 7 years ago

Let me know if it worked, might have some bugs or need some changes I just did it out of my head in a few minutes, did not test it.

Will definitely need to find some time for documentation.

rainstormza commented 7 years ago

@JonnyBGod

screen shot 2017-04-21 at 13 27 07

error like this happen after I 've tried your code :( any suggestion ? thanks

JonnyBGod commented 7 years ago

can you try moving the conde inside constructor to ngAfterviewinit?

rainstormza commented 7 years ago

@JonnyBGod yes, I do exactly the same as your code Could you share your source code ? thanks :)

anleo commented 7 years ago
Can't bind to 'scrollSpyIndex' since it isn't a known property of 'div'. ("
<div style="min-height: 200vh"
     scrollSpy
     [ERROR ->][scrollSpyIndex]="{id: 'contexts', selector: 'context'}"

Any suggestions?

Nexeuz commented 7 years ago

@anleo you have to import

import { ScrollSpyIndexModule } from 'ngx-scrollspy/dist/plugin';
import { ScrollSpyAffixModule } from 'ngx-scrollspy/dist/plugin/affix';

To your app.module.ts.

Borfswitch commented 6 years ago

I created a plunker that implements an example of this: https://plnkr.co/edit/7a2V6b5LMs3WGTw1HdVk?p=preview