Ionic get scroll position

In ionic 2, how do you get the current scroll position like in ionic version 1?

you can get the current ionContent / Scrollcontainer with the @ViewChild decorator of angular 2.

the content variable will have the current scroll coordinates?

you can call functions like getScrollTop

1 Like

ah, just tried it now. Thanks! I was looking for the scroll API under Scroll in the docs. Didn’t think it would be under content.

@bengtler Thank u so much, I was looking for that from a long time :smile:

one more thing though, with getScrollTop or setScrollTop you can access or set scrollTop position. But is there any way to trigger a function when at certain scrollTop position? or like a scroll event where you have access to scrollTop position at all times?

Thanks in advance!

not, yet i think.

But you can listen on all native dom-events

<div (scroll)="scrollPosition = $event.xxxx"></div>

I do not know exactly the correct key, where the scrollposition is stored on the event object.

1 Like

Yes, I already tried that, don’t know why but (scroll) event doesn’t seem to be working.
Not triggering the function :slight_frown:

Can you point me to the resourse, where you found that?

maybe you are not scrolling the ionContent --> there is a scroll-container in there, which scrolls :worried:

1 Like

okay, I’ll try using that … Thanks for your help!

You could use the addScrollListener as explained in

Here is a working Plunkr in case you have difficulties implementing it.
Just scroll away.

Hi ,

I am using an ion-scroll inside ion-toolbar with only horizontal scrolling enabled in IONIC 2. Scrolling is working smoothly. Is there any way to set the scroll position based on some condition?

Thanks
Anoop

In the latest Ionic version, ngAfterViewInit() in home.ts requires changing to:

this.content.ionScroll.subscribe(($event) => {
                this.scrollAmount = $event.scrollTop;
            });
2 Likes