forked from googlearchive/core-scroll-header-panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore-scroll-header-panel.html
281 lines (227 loc) · 7.74 KB
/
core-scroll-header-panel.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<!--
`core-scroll-header-panel` contains a header section and a content section. The
header is initially on the top part of the view but it scrolls away with the
rest of the scrollable content. Upon scrolling slightly up at any point, the
header scrolls back into view. This saves screen space and allows users to
access important controls by easily moving them back to the view.
__Important:__ The `core-scroll-header-panel` will not display if its parent does not have a height.
Using [layout attributes](http://www.polymer-project.org/docs/polymer/layout-attrs.html), you can easily make the `core-scroll-header-panel` fill the screen
<body fullbleed layout vertical>
<core-scroll-header-panel flex>
<core-toolbar>
<div>Hello World!</div>
</core-toolbar>
</core-scroll-header-panel>
</body>
or, if you would prefer to do it in CSS, just give `html`, `body`, and `core-scroll-header-panel` a height of 100%:
html, body {
height: 100%;
margin: 0;
}
core-scroll-header-panel {
height: 100%;
}
`core-scroll-header-panel` works well with `core-toolbar` but can use any element
that represents a header by adding a `core-header` class to it. Use the attribute
or class `content` to delineate the content section.
<core-scroll-header-panel>
<core-toolbar>Header</core-toolbar>
<div content>Content goes here...</div>
</core-scroll-header-panel>
@group Polymer Core Elements
@element core-scroll-header-panel
@homepage github.io
-->
<link rel="import" href="../polymer/polymer.html">
<polymer-element name="core-scroll-header-panel">
<template>
<link rel="stylesheet" href="core-scroll-header-panel.css">
<div id="mainContainer" on-scroll="{{scroll}}">
<content id="mainContent" select="[content], .content"></content>
</div>
<div id="headerContainer">
<div class="bg-container">
<div id="condensedHeaderBg"></div>
<div id="headerBg"></div>
</div>
<content id="headerContent" select="core-toolbar, .core-header"></content>
</div>
</template>
<script>
Polymer('core-scroll-header-panel', {
/**
* Fired when the content has been scrolled.
*
* @event scroll
*/
/**
* Fired when the header is transformed.
*
* @event core-header-transform
*/
publish: {
/**
* If true, the header's height will condense to `condensedHeaderHeight`
* as the user scrolls down from the top of the content area.
*
* @attribute condenses
* @type boolean
* @default false
*/
condenses: false,
/**
* If true, no cross-fade transition from one background to another.
*
* @attribute noDissolve
* @type boolean
* @default false
*/
noDissolve: false,
/**
* If true, the header doesn't slide back in when scrolling back up.
*
* @attribute noReveal
* @type boolean
* @default false
*/
noReveal: false,
/**
* If true, the header is fixed to the top and never moves away.
*
* @attribute fixed
* @type boolean
* @default false
*/
fixed: false,
/**
* If true, the condensed header is always shown and does not move away.
*
* @attribute keepCondensedHeader
* @type boolean
* @default false
*/
keepCondensedHeader: false,
/**
* The height of the header when it is at its full size.
*
* By default, the height will be measured when it is ready. If the height
* changes later the user needs to either set this value to reflect the
* new height or invoke `measureHeaderHeight()`.
*
* @attribute headerHeight
* @type number
*/
headerHeight: 0,
/**
* The height of the header when it is condensed.
*
* By default, this will be 1/3 of `headerHeight`.
*
* @attribute condensedHeaderHeight
* @type number
*/
condensedHeaderHeight: 0,
},
prevScrollTop: 0,
headerMargin: 0,
y: 0,
observe: {
'headerMargin fixed': 'setup'
},
domReady: function() {
this.async('measureHeaderHeight');
},
get header() {
return this.$.headerContent.getDistributedNodes()[0];
},
get scroller() {
return this.$.mainContainer;
},
measureHeaderHeight: function() {
var header = this.header;
if (this.header) {
this.headerHeight = header.offsetHeight;
}
},
headerHeightChanged: function() {
if (this.condensedHeaderHeight) {
this.condensedHeaderHeightChanged();
} else {
// assume condensedHeaderHeight is 1/3 of the headerHeight
this.condensedHeaderHeight = this.headerHeight * 1 / 3;
}
},
condensedHeaderHeightChanged: function() {
if (this.headerHeight) {
this.headerMargin = this.headerHeight - this.condensedHeaderHeight;
}
},
setup: function() {
var s = this.scroller.style;
s.paddingTop = this.fixed ? null : this.headerHeight + 'px';
s.top = this.fixed ? this.headerHeight + 'px' : null;
if (this.fixed) {
this.transformHeader(0);
} else {
this.scroll();
}
},
transformHeader: function(y) {
var s = this.$.headerContainer.style;
this.translateY(s, -y);
if (this.condenses) {
// adjust top bar in core-header so the top bar stays at the top
if (this.header.$ && this.header.$.topBar) {
s = this.header.$.topBar.style;
this.translateY(s, Math.min(y, this.headerMargin));
}
// transition header bg
s = this.$.headerBg.style;
if (!this.noDissolve) {
s.opacity = (this.headerMargin - y) / this.headerMargin;
}
// adjust header bg so it stays at the center
this.translateY(s, y / 2);
// transition condensed header bg
if (!this.noDissolve) {
s = this.$.condensedHeaderBg.style;
s.opacity = y / this.headerMargin;
// adjust condensed header bg so it stays at the center
this.translateY(s, y / 2);
}
}
this.fire('core-header-transform',
{y: y, height: this.headerHeight, condensedHeight: this.condensedHeaderHeight});
},
translateY: function(s, y) {
s.transform = s.webkitTransform = 'translate3d(0, ' + y + 'px, 0)';
},
scroll: function() {
if (!this.header) {
return;
}
var sTop = this.scroller.scrollTop;
var y = Math.min(this.keepCondensedHeader ?
this.headerMargin : this.headerHeight, Math.max(0,
(this.noReveal ? sTop : this.y + sTop - this.prevScrollTop)));
if (this.condenses && this.prevScrollTop > sTop && sTop > this.headerMargin) {
y = Math.max(y, this.headerMargin);
}
if (!this.fixed && y !== this.y) {
requestAnimationFrame(this.transformHeader.bind(this, y));
}
this.prevScrollTop = sTop;
this.y = y;
this.fire('scroll', {target: this.scroller}, this, false);
}
});
</script>
</polymer-element>