forked from PolymerElements/app-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-box.html
More file actions
238 lines (200 loc) · 6.55 KB
/
app-box.html
File metadata and controls
238 lines (200 loc) · 6.55 KB
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
<!--
@license
Copyright (c) 2015 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
-->
<link rel="import" href="../../polymer/polymer.html">
<link rel="import" href="../../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../../iron-resizable-behavior/iron-resizable-behavior.html">
<link rel="import" href="../app-scroll-effects/app-scroll-effects-behavior.html">
<!--
app-box is a container element that can have scroll effects - visual effects based on
scroll position. For example, the parallax effect can be used to move an image at a slower
rate than the foreground.
```html
<app-box style="height: 100px;" effects="parallax-background">
<img background src="picture.png" style="width: 100%; height: 600px;">
</app-box>
```
Notice the `background` attribute in the `img` element; this attribute specifies that that
image is used as the background. By adding the background to the light dom, you can compose
backgrounds that can change dynamically. Alternatively, the mixin `--app-box-background-front-layer`
allows to style the background. For example:
```css
.parallaxAppBox {
--app-box-background-front-layer: {
background-image: url(picture.png);
};
}
```
Finally, app-box can have content inside. For example:
```html
<app-box effects="parallax-background">
<h2>Sub title</h2>
</app-box>
```
#### Importing the effects
To use the scroll effects, you must explicitly import them in addition to `app-box`:
```html
<link rel="import" href="/bower_components/app-layout/app-scroll-effects/app-scroll-effects.html">
```
#### List of effects
* **parallax-background**
A simple parallax effect that vertically translates the backgrounds based on a fraction
of the scroll position. For example:
```css
app-header {
--app-header-background-front-layer: {
background-image: url(...);
};
}
```
```html
<app-header style="height: 300px;" effects="parallax-background">
<app-toolbar>App name</app-toolbar>
</app-header>
```
The fraction determines how far the background moves relative to the scroll position.
This value can be assigned via the `scalar` config value and it is typically a value
between 0 and 1 inclusive. If `scalar=0`, the background doesn't move away from the header.
## Styling
Mixin | Description | Default
----------------|-------------|----------
`--app-box-background-front-layer` | Applies to the front layer of the background | {}
@group App Elements
@element app-box
@demo app-box/demo/document-scroll.html Document Scroll
@demo app-box/demo/scrolling-region.html Scrolling Region
-->
<dom-module id="app-box">
<template>
<style>
:host {
position: relative;
display: block;
}
#background {
@apply(--layout-fit);
overflow: hidden;
height: 100%;
}
#backgroundFrontLayer {
min-height: 100%;
pointer-events: none;
background-size: cover;
@apply(--app-box-background-front-layer);
}
#contentContainer {
position: relative;
width: 100%;
height: 100%;
}
:host([disabled]),
:host([disabled]) #backgroundFrontLayer {
transition: none !important;
}
</style>
<div id="background">
<div id="backgroundFrontLayer">
<content select="[background]"></content>
</div>
</div>
<div id="contentContainer">
<content id="content"></content>
</div>
</template>
<script>
Polymer({
is: 'app-box',
behaviors: [
Polymer.AppScrollEffectsBehavior,
Polymer.IronResizableBehavior
],
listeners: {
'iron-resize': '_resizeHandler'
},
/**
* The current scroll progress.
*
* @type {number}
*/
_progress: 0,
attached: function() {
this.resetLayout();
},
/**
* Resets the layout. This method is automatically called when the element is attached to the DOM.
*
* @method resetLayout
*/
resetLayout: function() {
this.debounce('_resetLayout', function() {
// noop if the box isn't in the rendered tree
if (this.offsetWidth === 0 && this.offsetHeight === 0) {
return;
}
var scrollTop = this._clampedScrollTop;
var savedDisabled = this.disabled;
this.disabled = true;
this._elementTop = this._getElementTop();
this._elementHeight = this.offsetHeight;
this._cachedScrollTargetHeight = this._scrollTargetHeight;
this._setUpEffect();
this._updateScrollState(scrollTop);
this.disabled = savedDisabled;
}, 1);
},
_getElementTop: function() {
var currentNode = this;
var top = 0;
while (currentNode && currentNode !== this.scrollTarget) {
top += currentNode.offsetTop;
currentNode = currentNode.offsetParent;
}
return top;
},
_updateScrollState: function(scrollTop) {
if (this.isOnScreen()) {
var viewportTop = this._elementTop - scrollTop;
this._progress = 1 - (viewportTop + this._elementHeight) / this._cachedScrollTargetHeight;
this._runEffects(this._progress, scrollTop);
}
},
/**
* Returns true if this app-box is on the screen.
* That is, visible in the current viewport.
*
* @method isOnScreen
* @return {boolean}
*/
isOnScreen: function() {
return this._elementTop < this._scrollTop + this._cachedScrollTargetHeight
&& this._elementTop + this._elementHeight > this._scrollTop;
},
_resizeHandler: function() {
this.resetLayout();
},
_getDOMRef: function(id) {
if (id === 'background') {
return this.$.background;
}
if (id === 'backgroundFrontLayer') {
return this.$.backgroundFrontLayer;
}
},
/**
* Returns an object containing the progress value of the scroll effects.
*
* @method getScrollState
* @return {Object}
*/
getScrollState: function() {
return { progress: this._progress };
}
});
</script>
</dom-module>