9

Hi everyone i am trying to add "scroll to top button." that implement the following :

1.Show the button when user has scrolled down. 2.Hide the button when the user scrolls up. 3.If the button is tapped then scroll to the top and hide the button . any suggestion for how make it right way?

thanks a lot

David
  • 93
  • 1
  • 1
  • 7

5 Answers5

9

Simplifying the scrollToTop() from adriancarriger

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

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

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

Source: http://ionicframework.com/docs/v2/api/components/content/Content/

7

Plunker Demo

To make this work you need to:

  • Create a function that scrolls your scroll-content element to the top
  • Track the scroll position of scroll-content
  • Use *ngIf on your scroll to top button to conditionally show after scroll-content has reached a certain threshold.

Scroll to top function

I adapted this SO answer to apply to the scroll-content element

scrollToTop(scrollDuration) {
let scrollStep = -this.ionScroll.scrollTop / (scrollDuration / 15);
let scrollInterval = setInterval( () => {
    if ( this.ionScroll.scrollTop != 0 ) {
        this.ionScroll.scrollTop = this.ionScroll.scrollTop + scrollStep;
    } else {
      clearInterval(scrollInterval);
    }
}, 15);

Track scroll-content position

This example uses the window height as the threshold for showing the scroll to top button like this:

this.ionScroll.addEventListener("scroll", () => {
  if (this.ionScroll.scrollTop > window.innerHeight) {
    this.showButton = true;
  } else {
    this.showButton = false;
  }
});

Button Html

<button *ngIf="showButton" (click)="scrollToTop(1000)">Scroll Top</button>

Full component Typescript

import { NavController } from 'ionic-angular/index';
import { Component, OnInit, ElementRef } from "@angular/core";

@Component({
  templateUrl:"home.html"
})
export class HomePage implements OnInit {
  public ionScroll;
  public showButton = false;
  public contentData = [];

  constructor(public myElement: ElementRef) {}

  ngOnInit() {
    // Ionic scroll element
    this.ionScroll = this.myElement.nativeElement.children[1].firstChild;
    // On scroll function
    this.ionScroll.addEventListener("scroll", () => {
      if (this.ionScroll.scrollTop > window.innerHeight) {
        this.showButton = true;
      } else {
        this.showButton = false;
      }
    });
    // Content data
    for (let i = 0; i < 301; i++) {
      this.contentData.push(i);
    }
  }
  // Scroll to top function
  // Adapted from https://stackoverflow.com/a/24559613/5357459
  scrollToTop(scrollDuration) {
    let scrollStep = -this.ionScroll.scrollTop / (scrollDuration / 15);
    let scrollInterval = setInterval( () => {
        if ( this.ionScroll.scrollTop != 0 ) {
            this.ionScroll.scrollTop = this.ionScroll.scrollTop + scrollStep;
        } else {
          clearInterval(scrollInterval);
        }
    }, 15);
  }

}

Full component Html

<ion-navbar primary *navbar>
  <ion-title>
    Ionic 2
  </ion-title>
  <button *ngIf="showButton" (click)="scrollToTop(1000)">Scroll Top</button>
</ion-navbar>

<ion-content class="has-header" #testElement>
  <div padding style="text-align: center;">
    <h1>Ionic 2 Test</h1>
    <div *ngFor="let item of contentData">
      test content-{{item}}
    </div>
  </div>
</ion-content>
Community
  • 1
  • 1
adriancarriger
  • 2,870
  • 3
  • 16
  • 25
  • woooow thank you very much for the professional answer but i got a little error :EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot read property 'firstChild' of undefined – David Jul 13 '16 at 06:50
  • this.ionScroll = this.myElement.nativeElement.children[0].firstChild; solved the problem – David Jul 13 '16 at 06:56
2

You could try this. Hoping it helps

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

import { Content } from 'ionic-angular';

@Component({...})

export class MyPage{

  @ViewChild(Content) content: Content;

  scrollToTop() {

    this.content.scrollToTop();
  }

}
Elias MP
  • 1,829
  • 9
  • 29
  • 47
jagadeesh
  • 17
  • 2
0

With the help of adriancarriger code, I tried the following to set Div element to Top and it worked. Below is my code

Html

<div  #Innholdtext >
<p> add your contents here to set scroll top </>
</div>

app.component.ts

import { Component, OnInit,  ViewChild, ElementRef } from '@angular/core';
export class app implements OnInit {

@ViewChild('Innholdtext') private hovedInnhold : ElementRef;
    private scrollElement; 
}

   ngOnInit() {
  this.scrollElement = this.hovedInnhold.nativeElement;
        this.scrollElement.addEventListener("click", () =>{
            this.hovedInnhold.nativeElement.scrollTop = 0
        });
}
Amr ElAdawy
  • 3,663
  • 5
  • 31
  • 48
Savitha
  • 1
  • 1
0

When u have a component inside a component, you might have problems getting the Content inside the inner component. That said i had to get the upper element and call to scroll.

In my case was when going to the next slide, i had to be on top for the next slide.

 const element = document.querySelector(".slide-zoom");
      setTimeout(() => { if(element) element.scrollIntoView(true) }, 200); 
mariomol
  • 597
  • 1
  • 6
  • 14