Skip to content

Commit 285b7cd

Browse files
authored
clean up scroll to midpoint logic (PR #73)
- use bounding client rect instead of shorthand properties - account for existing scroll offset
1 parent eb27942 commit 285b7cd

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/js/01-nav.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@
150150
e.stopPropagation()
151151
}
152152

153-
function scrollItemToMidpoint (panel, el) {
154-
var rect = panel.getBoundingClientRect()
155-
var effectiveHeight = rect.height
156-
var navStyle = window.getComputedStyle(nav)
157-
if (navStyle.position === 'sticky') effectiveHeight -= rect.top - parseFloat(navStyle.top)
158-
panel.scrollTop = Math.max(0, (el.getBoundingClientRect().height - effectiveHeight) * 0.5 + el.offsetTop)
153+
function scrollItemToMidpoint (panel, item) {
154+
var itemRect = item.getBoundingClientRect()
155+
var panelRect = panel.getBoundingClientRect()
156+
var currentOffsetTop = itemRect.top - panelRect.top + panel.scrollTop
157+
var targetOffsetTop = (panelRect.height - itemRect.height) * 0.5
158+
panel.scrollTop = Math.max(0, (currentOffsetTop - targetOffsetTop).toFixed())
159159
}
160160

161161
function find (from, selector) {

0 commit comments

Comments
 (0)