简体   繁体   中英

ionic 2 - Get window.scrollTop

How can I do this in Ionic without jquery?

  var window_top = jQuery(window).scrollTop() + 56;
  var div_top = jQuery('#sticky-anchor').offset().top;

You can use a reference to the Content :

import { Component, ViewChild } from '@angular/core';
import { Content } from 'ionic-angular';

@Component({...})
export class MyPage{
  @ViewChild(Content) content: Content;

  scrollToTop() {
    this.content.scrollToTop();
  }

  scrollTo(elementId:string) {
    let yOffset = document.getElementById(elementId).offsetTop;
    this.content.scrollTo(0, yOffset, 4000)
  }
}

There are a lot of properties that you can use; one of the most importants is...

getContentDimensions() : Returns the content and scroll elements' dimensions.

Property                    Type    Details
dimensions.contentHeight    number  content offsetHeight
dimensions.contentTop       number  content offsetTop
dimensions.contentBottom    number  content offsetTop+offsetHeight
dimensions.contentWidth     number  content offsetWidth
dimensions.contentLeft      number  content offsetLeft
dimensions.contentRight     number  content offsetLeft + offsetWidth
dimensions.scrollHeight     number  scroll scrollHeight
dimensions.scrollTop        number  scroll scrollTop
dimensions.scrollBottom     number  scroll scrollTop + scrollHeight
dimensions.scrollWidth      number  scroll scrollWidth
dimensions.scrollLeft       number  scroll scrollLeft
dimensions.scrollRight      number  scroll scrollLeft + scrollWidth

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM