ngx-scrollspy icon indicating copy to clipboard operation
ngx-scrollspy copied to clipboard

Does Spy any element scroll can do this ?

Open rainstormza opened this issue 7 years ago • 9 comments

1492740850034

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

thanks

rainstormza avatar Apr 21 '17 02:04 rainstormza

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$.

JonnyBGod avatar Apr 21 '17 03:04 JonnyBGod

@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 👍

rainstormza avatar Apr 21 '17 03:04 rainstormza

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.

JonnyBGod avatar Apr 21 '17 03:04 JonnyBGod

@JonnyBGod screen shot 2017-04-21 at 13 27 07

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

rainstormza avatar Apr 21 '17 06:04 rainstormza

can you try moving the conde inside constructor to ngAfterviewinit?

JonnyBGod avatar Apr 21 '17 16:04 JonnyBGod

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

rainstormza avatar Apr 22 '17 02:04 rainstormza

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?

anleo avatar Jul 01 '17 17:07 anleo

@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.

Nexeuz avatar Sep 07 '17 19:09 Nexeuz

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

Borfswitch avatar Dec 14 '17 17:12 Borfswitch