Skip to content

Commit

Permalink
fix scroll into view behavior in navigation
Browse files Browse the repository at this point in the history
- set position relative on .nav-menu
- remove position relative from .nav-item
- optimize computations for scroll into view
- remove debug statements
  • Loading branch information
mojavelinux committed Aug 13, 2018
1 parent c7a96c4 commit 7b83570
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/css/navigation-menu.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ html.is-clipped--nav {
width: 100%;
padding: 0.5rem 0.75rem;
line-height: 1.35;
position: relative;
}

.nav-menu h3.title {
Expand All @@ -64,7 +65,6 @@ html.is-clipped--nav {

.nav-item {
list-style: none;
position: relative;
margin-top: 0.5em;
}

Expand Down
15 changes: 6 additions & 9 deletions src/js/01-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,19 @@
}

function scrollItemIntoView (scrollPosition, parent, el) {
console.log('scrolling item into view')
console.log('stored value: ' + scrollPosition)
if (!el) return (parent.scrollTop = scrollPosition)

var margin = 10
var overTheTop = el.offsetTop - scrollPosition < 0
var belowTheBottom = (el.offsetTop - scrollPosition + el.offsetHeight) > parent.offsetHeight
//var y = el.getBoundingClientRect().top - parent.getBoundingClientRect().top
var y = el.offsetTop

if (overTheTop) {
parent.scrollTop = el.offsetTop - margin
} else if (belowTheBottom) {
parent.scrollTop = el.offsetTop - (parent.offsetHeight - el.offsetHeight) + margin
if (y < scrollPosition) {
parent.scrollTop = y - margin
} else if (y - parent.offsetHeight + el.offsetHeight > scrollPosition) {
parent.scrollTop = y - parent.offsetHeight + el.offsetHeight + margin
} else {
parent.scrollTop = scrollPosition
}
console.log('set scrollTop to ' + parent.scrollTop)
}

function find (selector, from) {
Expand Down

0 comments on commit 7b83570

Please sign in to comment.