Ionic get scroll position


#1

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


#2

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


#3

the content variable will have the current scroll coordinates?


#4

you can call functions like getScrollTop


#5

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.


#6

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


#7

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.


#8

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?


#9

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


#10

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


#11

You could use the addScrollListener as explained in

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


#12

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


#13

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

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