OLD | NEW |
(Empty) | |
| 1 |
| 2 |
| 3 Polymer('core-scroll-header-panel', { |
| 4 |
| 5 /** |
| 6 * Fired when the content has been scrolled. |
| 7 * |
| 8 * @event scroll |
| 9 */ |
| 10 |
| 11 /** |
| 12 * Fired when the header is transformed. |
| 13 * |
| 14 * @event core-header-transform |
| 15 */ |
| 16 |
| 17 publish: { |
| 18 /** |
| 19 * If true, the header's height will condense to `_condensedHeaderHeight` |
| 20 * as the user scrolls down from the top of the content area. |
| 21 * |
| 22 * @attribute condenses |
| 23 * @type boolean |
| 24 * @default false |
| 25 */ |
| 26 condenses: false, |
| 27 |
| 28 /** |
| 29 * If true, no cross-fade transition from one background to another. |
| 30 * |
| 31 * @attribute noDissolve |
| 32 * @type boolean |
| 33 * @default false |
| 34 */ |
| 35 noDissolve: false, |
| 36 |
| 37 /** |
| 38 * If true, the header doesn't slide back in when scrolling back up. |
| 39 * |
| 40 * @attribute noReveal |
| 41 * @type boolean |
| 42 * @default false |
| 43 */ |
| 44 noReveal: false, |
| 45 |
| 46 /** |
| 47 * If true, the header is fixed to the top and never moves away. |
| 48 * |
| 49 * @attribute fixed |
| 50 * @type boolean |
| 51 * @default false |
| 52 */ |
| 53 fixed: false, |
| 54 |
| 55 /** |
| 56 * If true, the condensed header is always shown and does not move away. |
| 57 * |
| 58 * @attribute keepCondensedHeader |
| 59 * @type boolean |
| 60 * @default false |
| 61 */ |
| 62 keepCondensedHeader: false, |
| 63 |
| 64 /** |
| 65 * The height of the header when it is at its full size. |
| 66 * |
| 67 * By default, the height will be measured when it is ready. If the heigh
t |
| 68 * changes later the user needs to either set this value to reflect the |
| 69 * new height or invoke `measureHeaderHeight()`. |
| 70 * |
| 71 * @attribute headerHeight |
| 72 * @type number |
| 73 */ |
| 74 headerHeight: 0, |
| 75 |
| 76 /** |
| 77 * The height of the header when it is condensed. |
| 78 * |
| 79 * By default, `_condensedHeaderHeight` is 1/3 of `headerHeight` unless |
| 80 * this is specified. |
| 81 * |
| 82 * @attribute condensedHeaderHeight |
| 83 * @type number |
| 84 */ |
| 85 condensedHeaderHeight: 0 |
| 86 }, |
| 87 |
| 88 prevScrollTop: 0, |
| 89 |
| 90 headerMargin: 0, |
| 91 |
| 92 y: 0, |
| 93 |
| 94 observe: { |
| 95 'headerMargin fixed': 'setup' |
| 96 }, |
| 97 |
| 98 domReady: function() { |
| 99 this.async('measureHeaderHeight'); |
| 100 }, |
| 101 |
| 102 get header() { |
| 103 return this.$.headerContent.getDistributedNodes()[0]; |
| 104 }, |
| 105 |
| 106 get scroller() { |
| 107 return this.$.mainContainer; |
| 108 }, |
| 109 |
| 110 measureHeaderHeight: function() { |
| 111 var header = this.header; |
| 112 if (this.header) { |
| 113 this.headerHeight = header.offsetHeight; |
| 114 } |
| 115 }, |
| 116 |
| 117 headerHeightChanged: function() { |
| 118 if (!this.condensedHeaderHeight) { |
| 119 // assume _condensedHeaderHeight is 1/3 of the headerHeight |
| 120 this._condensedHeaderHeight = this.headerHeight * 1 / 3; |
| 121 } |
| 122 this.condensedHeaderHeightChanged(); |
| 123 }, |
| 124 |
| 125 condensedHeaderHeightChanged: function() { |
| 126 if (this.condensedHeaderHeight) { |
| 127 this._condensedHeaderHeight = this.condensedHeaderHeight; |
| 128 } |
| 129 if (this.headerHeight) { |
| 130 this.headerMargin = this.headerHeight - this._condensedHeaderHeight; |
| 131 } |
| 132 }, |
| 133 |
| 134 condensesChanged: function() { |
| 135 if (this.condenses) { |
| 136 this.scroll(); |
| 137 } else { |
| 138 // reset transform/opacity set on the header |
| 139 this.condenseHeader(null); |
| 140 } |
| 141 }, |
| 142 |
| 143 setup: function() { |
| 144 var s = this.scroller.style; |
| 145 s.paddingTop = this.fixed ? '' : this.headerHeight + 'px'; |
| 146 s.top = this.fixed ? this.headerHeight + 'px' : ''; |
| 147 if (this.fixed) { |
| 148 this.transformHeader(null); |
| 149 } else { |
| 150 this.scroll(); |
| 151 } |
| 152 }, |
| 153 |
| 154 transformHeader: function(y) { |
| 155 var s = this.$.headerContainer.style; |
| 156 this.translateY(s, -y); |
| 157 |
| 158 if (this.condenses) { |
| 159 this.condenseHeader(y); |
| 160 } |
| 161 |
| 162 this.fire('core-header-transform', {y: y, height: this.headerHeight, |
| 163 condensedHeight: this._condensedHeaderHeight}); |
| 164 }, |
| 165 |
| 166 condenseHeader: function(y) { |
| 167 var reset = y == null; |
| 168 // adjust top bar in core-header so the top bar stays at the top |
| 169 if (this.header.$ && this.header.$.topBar) { |
| 170 this.translateY(this.header.$.topBar.style, |
| 171 reset ? null : Math.min(y, this.headerMargin)); |
| 172 } |
| 173 // transition header bg |
| 174 var hbg = this.$.headerBg.style; |
| 175 if (!this.noDissolve) { |
| 176 hbg.opacity = reset ? '' : (this.headerMargin - y) / this.headerMargin; |
| 177 } |
| 178 // adjust header bg so it stays at the center |
| 179 this.translateY(hbg, reset ? null : y / 2); |
| 180 // transition condensed header bg |
| 181 var chbg = this.$.condensedHeaderBg.style; |
| 182 if (!this.noDissolve) { |
| 183 chbg = this.$.condensedHeaderBg.style; |
| 184 chbg.opacity = reset ? '' : y / this.headerMargin; |
| 185 // adjust condensed header bg so it stays at the center |
| 186 this.translateY(chbg, reset ? null : y / 2); |
| 187 } |
| 188 }, |
| 189 |
| 190 translateY: function(s, y) { |
| 191 s.transform = s.webkitTransform = y == null ? '' : |
| 192 'translate3d(0, ' + y + 'px, 0)'; |
| 193 }, |
| 194 |
| 195 scroll: function(event) { |
| 196 if (!this.header) { |
| 197 return; |
| 198 } |
| 199 |
| 200 var sTop = this.scroller.scrollTop; |
| 201 |
| 202 var y = Math.min(this.keepCondensedHeader ? |
| 203 this.headerMargin : this.headerHeight, Math.max(0, |
| 204 (this.noReveal ? sTop : this.y + sTop - this.prevScrollTop))); |
| 205 |
| 206 if (this.condenses && this.prevScrollTop >= sTop && sTop > this.headerMarg
in) { |
| 207 y = Math.max(y, this.headerMargin); |
| 208 } |
| 209 |
| 210 if (!event || !this.fixed && y !== this.y) { |
| 211 requestAnimationFrame(this.transformHeader.bind(this, y)); |
| 212 } |
| 213 |
| 214 this.prevScrollTop = sTop; |
| 215 this.y = y; |
| 216 |
| 217 if (event) { |
| 218 this.fire('scroll', {target: this.scroller}, this, false); |
| 219 } |
| 220 } |
| 221 |
| 222 }); |
| 223 |
OLD | NEW |