Content
ion-content
ion-content
The Content component provides an easy to use content area with some useful methods to control the scrollable area. There should only be one content in a single view component. If additional scrollable elements are need, use ionScroll.
The content area can also implement pull-to-refresh with the Refresher component.
Usage
<ion-content>
Add your content here!
</ion-content>
To get a reference to the content component from a Page's logic,
you can use Angular's @ViewChild
annotation:
import { Component, ViewChild } from '@angular/core';
import { Content } from 'ionic-angular';
@Component({...})
export class MyPage{
@ViewChild(Content) content: Content;
scrollToTop() {
this.content.scrollToTop();
}
}
Instance Members
addImg()
contentBottom
A number representing how many pixels the bottom of the content has been adjusted, which could be by either padding or margin. This adjustment is to account for the space needed for the footer.
number
contentHeight
Content height of the viewable area. This does not include content which is outside the overflow area, or content area which is under headers and footers. Read-only.
number
contentTop
A number representing how many pixels the top of the content has been adjusted, which could be by either padding or margin. This adjustment is to account for the space needed for the header.
number
contentWidth
Content width including content which is not visible on the screen due to overflow. Read-only.
number
directionX
The current, or last known, horizontal scroll direction. Possible
string values include right
and left
.
string
directionY
The current, or last known, vertical scroll direction. Possible
string values include down
and up
.
string
getContentDimensions()
Returns the content and scroll elements’ dimensions.
object
dimensions 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 |
getFixedElement()
isScrolling
If the content is actively scrolling or not.
boolean
resize()
Tell the content to recalculate its dimensions. This should be called after dynamically adding/removing headers, footers, or tabs.
scrollHeight
Content height including content which is not visible on the screen due to overflow. Read-only.
number
scrollLeft
The distance of the content’s left to its leftmost visible content.
number
scrollTo(x, y, duration)
Scroll to the specified position.
Param | Type | Details |
---|---|---|
x |
number
|
The x-value to scroll to. |
y |
number
|
The y-value to scroll to. |
duration |
number
|
Duration of the scroll animation in milliseconds. Defaults to |
Promise
Returns a promise which is resolved when the scroll has completed.
scrollToBottom(duration)
Scroll to the bottom of the content component.
Param | Type | Details |
---|---|---|
duration |
number
|
Duration of the scroll animation in milliseconds. Defaults to |
Promise
Returns a promise which is resolved when the scroll has completed.
scrollToTop(duration)
Scroll to the top of the content component.
Param | Type | Details |
---|---|---|
duration |
number
|
Duration of the scroll animation in milliseconds. Defaults to |
Promise
Returns a promise which is resolved when the scroll has completed.
scrollTop
The distance of the content’s top to its topmost visible content.
number
scrollWidth
Content width including content which is not visible due to overflow. Read-only.
number
Input Properties
Attr | Type | Details |
---|---|---|
fullscreen | boolean |
If true, the content will scroll behind the headers and footers. This effect can easily be seen by setting the toolbar to transparent. |
scrollDownOnLoad | boolean |
If true, the content will scroll down on load. |
Output Events
Attr | Details |
---|---|
ionScroll | Emitted on every scroll event. |
ionScrollEnd | Emitted when scrolling ends. |
ionScrollStart | Emitted when the scrolling first starts. |
Advanced
Scroll Events
Scroll events happen outside of Angular's Zones. This is for performance reasons. So
if you're trying to bind a value to any scroll event, it will need to be wrapped in
a zone.run()
import { Component, NgZone } from '@angular/core';
@Component({
template: `
<ion-header>
<ion-navbar>
<ion-title></ion-title>
</ion-navbar>
</ion-header>
<ion-content (ionScroll)="scrollHandler($event)">
<p> Some realllllllly long content </p>
</ion-content>
`})
class E2EPage {
public scrollAmount = 0;
constructor( public zone: NgZone){}
scrollHandler(event) {
console.log(`ScrollEvent: ${event}`)
this.zone.run(()=>{
// since scrollAmount is data-binded,
// the update needs to happen in zone
this.scrollAmount++
})
}
}
This goes for any scroll event, not just ionScroll
.
Resizing the content
If the height of ion-header
, ion-footer
or ion-tabbar
changes dynamically, content.resize()
has to be called in order to update the
layout of Content
.
@Component({
template: `
<ion-header>
<ion-navbar>
<ion-title>Main Navbar</ion-title>
</ion-navbar>
<ion-toolbar *ngIf="showToolbar">
<ion-title>Dynamic Toolbar</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<button ion-button (click)="toggleToolbar()">Toggle Toolbar</button>
</ion-content>
`})
class E2EPage {
@ViewChild(Content) content: Content;
showToolbar: boolean = false;
toggleToolbar() {
this.showToolbar = !this.showToolbar;
this.content.resize();
}
}
Scroll to a specific position
import { Component, ViewChild } from '@angular/core';
import { Content } from 'ionic-angular';
@Component({
template: `<ion-content>
<button ion-button (click)="scrollTo()">Down 500px</button>
</ion-content>`
)}
export class MyPage{
@ViewChild(Content) content: Content;
scrollTo() {
// set the scrollLeft to 0px, and scrollTop to 500px
// the scroll duration should take 200ms
this.content.scrollTo(0, 500, 200);
}
}
Sass Variables
iOS
Property | Default | Description |
---|---|---|
$content-ios-outer-background |
#efeff4 |
Background color of the outer content |
$content-ios-transition-background |
#000 |
Background color of the content when making transition |