|
14 | 14 | var navBounds = { encroachingElement: document.querySelector('footer.footer') }
|
15 | 15 | var currentPageItem
|
16 | 16 |
|
17 |
| - window.addEventListener('load', fitNavInit) |
| 17 | + window.addEventListener('load', fitNavInit) /* needed if images shift the content */ |
18 | 18 | window.addEventListener('resize', fitNavInit)
|
19 | 19 |
|
20 | 20 | if (!menuPanel) return fitNavInit({})
|
|
164 | 164 | }
|
165 | 165 |
|
166 | 166 | function fitNavInit (e) {
|
167 |
| - if (e.type) { |
168 |
| - window.removeEventListener('scroll', fitNav) |
169 |
| - nav.style.height = '' |
170 |
| - } |
171 |
| - if (window.getComputedStyle(navContainer).position !== 'fixed') { |
172 |
| - navBounds.availableHeight = window.innerHeight |
173 |
| - navBounds.preferredHeight = nav.getBoundingClientRect().height |
174 |
| - fitNav() |
175 |
| - window.addEventListener('scroll', fitNav) |
176 |
| - } |
177 |
| - if (e.type !== 'resize' && currentPageItem) scrollItemToMidpoint(menuPanel, currentPageItem) |
| 167 | + window.removeEventListener('scroll', fitNav) |
| 168 | + if (window.getComputedStyle(navContainer).position === 'fixed') return |
| 169 | + navBounds.availableHeight = window.innerHeight |
| 170 | + navBounds.preferredHeight = navContainer.getBoundingClientRect().height |
| 171 | + if (fitNav() && e.type !== 'resize' && currentPageItem) scrollItemToMidpoint(menuPanel, currentPageItem) |
| 172 | + window.addEventListener('scroll', fitNav) |
178 | 173 | }
|
179 | 174 |
|
180 | 175 | function fitNav () {
|
181 | 176 | var scrollDatum = menuPanel && (menuPanel.scrollTop + menuPanel.offsetHeight)
|
182 |
| - var occupiedHeight = navBounds.availableHeight - navBounds.encroachingElement.getBoundingClientRect().top |
183 |
| - nav.style.height = occupiedHeight > 0 |
184 |
| - ? Math.max(0, Math.round(navBounds.preferredHeight - occupiedHeight)) + 'px' |
185 |
| - : '' |
| 177 | + var occupied = navBounds.availableHeight - navBounds.encroachingElement.getBoundingClientRect().top |
| 178 | + var modified = occupied > 0 |
| 179 | + ? nav.style.height !== (nav.style.height = Math.max(Math.round(navBounds.preferredHeight - occupied), 0) + 'px') |
| 180 | + : !!nav.style.removeProperty('height') |
186 | 181 | if (menuPanel) menuPanel.scrollTop = scrollDatum - menuPanel.offsetHeight
|
| 182 | + return modified |
187 | 183 | }
|
188 | 184 | })()
|
0 commit comments