Skip to content

Commit a1b47e8

Browse files
committed
simplify scroll item to midpoint logic
1 parent 030985d commit a1b47e8

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

src/js/01-nav.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,10 @@
151151
}
152152

153153
function scrollItemToMidpoint (panel, item) {
154-
var itemRect = item.getBoundingClientRect()
154+
if (panel.scrollHeight === panel.clientHeight) return // not scrollable
155155
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())
156+
var linkRect = item.getBoundingClientRect()
157+
panel.scrollTop += Math.round(linkRect.top - panelRect.top - (panelRect.height - linkRect.height) * 0.5)
159158
}
160159

161160
function find (from, selector) {

0 commit comments

Comments
 (0)