I need to get the scrollTop position in my Ionic 2 page, but it is just not working using simple jQuery.
Checkout my working example below, scroll the page and click on it, it will give you the y
position number.
$("body").click(function(){ var scrollPost = $(document).scrollTop(); alert(scrollPost); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/> test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/> <div class="test">Thats a test</div>
But jQuery method to get scrollTop does not work, it always return 0.
So I have planned to get it using the ionic way, documentation here
I am trying to use dimensions.contentHeight , dimensions.contentTop , but getting error. Below is my code.
home.html
code:
<ion-content content>
<div #topScroll>
The world is your oyster.
test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>
test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>test<br/><br/><br/><br/>kjh<br/>kjgh<br/>uitz<br/><br/><br/><br/>uztjhg<br/>kjh<br/>
<div class="test">Thats a test</div>
</div>
</ion-content>
home.ts
code updated
home.ts
code:
import { Content } from 'ionic-angular';
import { Component, ViewChild } from '@angular/core';
import { NavController } from 'ionic-angular';
import { AfterViewInit,Renderer2, ElementRef } from '@angular/core';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage implements AfterViewInit {
@ViewChild(Content) content: Content;
constructor(private renderer: Renderer2, public navCtrl: NavController) {
}
ngAfterViewInit() {
// Renderer2 used not Renderer, inject it before using
this.renderer.listen('document', 'click', (e) => {
// Get contentHeight here
var iscroll = this.content.contentHeight;
// error - Property 'dimensions' does not exist on type 'Content'
var iscroll2 = this. content.dimensions.contentHeight;
alert(iscroll);
alert(iscroll2);
});
}
Error coming - cannot read property
contentHeight
of Undefined.
What am I doing wrong?
You have multiple issues here:
content
and not #content
in the template I'd do:
<ion-content #content><!-- omitted content --></ion-content>
// Make sure to import AfterViewInit and implement it
ngAfterViewInit() {
// Renderer2 used not Renderer, inject it before using
this.renderer.listen('document', 'click', (e) => {
// Get contentHeight here
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.