How to scroll with ionic and have fixed content above
There is now in Ionic attribute slot
. You can use it like that:
<my-video slot="fixed"></my-video>
It's shortly mentioned on Ionic documentation here:
https://ionicframework.com/docs/api/content#slots
- Move your video directive out of ion-content (all elements in ion-content will be scrolled).
- Change the css of ion-content so that it does not occupy the upper half of the screen.
- Add
position:fixed
and other css to your video directive, so that it takes up the upper half of your screen.
HTML:
<html ng-app="ionicApp">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>Ionic Fixed Element at Top</title>
<link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
<script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
</head>
<body ng-controller="MyCtrl">
<ion-header-bar class="bar-positive">
<h1 class="title">Ionic Fixed Element at Top</h1>
</ion-header-bar>
<div class="fixed-header my-video">
My fixed video content
</div>
<ion-content class="ion-content-below-my-video">
<ion-list>
<ion-item>1</ion-item>
<ion-item>2</ion-item>
<ion-item>3</ion-item>
<ion-item>4</ion-item>
<ion-item>5</ion-item>
<ion-item>6</ion-item>
<ion-item>7</ion-item>
<ion-item>8</ion-item>
<ion-item>9</ion-item>
<ion-item>0</ion-item>
<ion-item>1</ion-item>
<ion-item>2</ion-item>
<ion-item>3</ion-item>
<ion-item>4</ion-item>
<ion-item>5</ion-item>
<ion-item>6</ion-item>
<ion-item>7</ion-item>
<ion-item>8</ion-item>
<ion-item>9</ion-item>
<ion-item>0</ion-item>
</ion-list>
</ion-content>
</body>
</html>
CSS:
body {
cursor: url('http://ionicframework.com/img/finger.png'), auto;
}
.fixed-header{
margin-top:43px;
/*
The ionic header bar is 43px in height,
put your content below the header bar.
*/
}
.platform-webview.platform-ios.platform-cordova .fixed-header{
margin-top:63px;
/* On iOS, moving a div out of ion-content,
I think you need to compensation for the 20px system status bar.
so it's 43px+20px=63px. but I'm not so sure, it hasn't been tested.
Test it yourself and make modifications if needed.
*/
}
.my-video
{
height:200px;
width:100%;
position:absolute;
background:grey;
color:red;
text-align:center;
padding:20px;
}
.ion-content-below-my-video{
margin-top:200px!important;
}
.platform-webview.platform-ios.platform-cordova .ion-content-below-my-video{
margin-top:220px;
/*
Same as .fixed-header
*/
}
Code pen here:
http://codepen.io/KevinWang15/pen/QNJEXX
In Ionic4, you can use the slot="fixed"
property for children of <ion-content>
to achieve this.
Check the docs
You can use ion-fixed
: (Ionic 3.5.0)
<ion-content no-padding>
<div ion-fixed class="map"></div>
<div ion-fixed class="test_class">
<h4>Bla Bla</h4>
</div>
<ion-list>
<ion-item>
<p>Test</p>
</ion-item>
</ion-list>
</ion-content>
Then on css:
.map {
width: 100%;
overflow: hidden;
height: 30%;
}
.test_class {
width: 100%;
height: 30px;
top: 30%;
}
Ionic documentation has no reference on this one, yet.