Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable scroll container #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
node_modules
*.log
maps
.idea
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jump('.target', {
duration: 1000,
offset: 0,
callback: undefined,
container: window,
easing: easeInOutQuad,
a11y: false
})
Expand All @@ -67,6 +68,7 @@ Explanation of each option follows:
* [duration](#duration)
* [offset](#offset)
* [callback](#callback)
* [container](#container)
* [easing](#easing)
* [a11y](#a11y)

Expand Down Expand Up @@ -152,6 +154,17 @@ jump('.target', {
})
```

### container

Pass a custom container element instead of scrolling `window`. The option
`container` can be a CSS selector or a node.

```es6
jump('.target', {
container: '#myScrollableContainer'
})
```

### easing

Easing function used to transition the `jump()`.
Expand Down
37 changes: 33 additions & 4 deletions src/jump.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const jumper = () => {
// private variable cache
// no variables are created during a jump, preventing memory leaks

let container // container element to be scrolled (node)
let element // element to scroll to (node)

let start // where scroll starts (px)
Expand All @@ -26,13 +27,26 @@ const jumper = () => {
// scroll position helper

function location() {
return window.scrollY || window.pageYOffset
return container.scrollY || container.pageYOffset || container.scrollTop
}

// element offset helper

function top(element) {
return element.getBoundingClientRect().top + start
const elementTop = element.getBoundingClientRect().top
const containerTop = container.getBoundingClientRect
? container.getBoundingClientRect().top
: 0

return elementTop - containerTop + start
}

// scrollTo helper

function scrollTo(top) {
container.scrollTo
? container.scrollTo(0, top) // window
: container.scrollTop = top // custom container
}

// rAF loop helper
Expand All @@ -50,7 +64,7 @@ const jumper = () => {
next = easing(timeElapsed, start, distance, duration)

// scroll to it
window.scrollTo(0, next)
scrollTo(next)

// check progress
timeElapsed < duration
Expand All @@ -62,7 +76,7 @@ const jumper = () => {

function done() {
// account for rAF time rounding inaccuracies
window.scrollTo(0, start + distance)
scrollTo(start + distance)

// if scrolling to an element, and accessibility is enabled
if(element && a11y) {
Expand Down Expand Up @@ -92,6 +106,21 @@ const jumper = () => {
easing = options.easing || easeInOutQuad
a11y = options.a11y || false

// resolve container
switch(typeof options.container) {
case 'object':
// we assume container is an HTML element (Node)
container = options.container
break

case 'string':
container = document.querySelector(options.container)
Copy link

@mauskin mauskin May 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn’t it be better to look up the container going from the target up instead of from the document down?

container = target.closest(options.container)

It would exclude errors where the container selector would be a common class name instead of id. Also might be faster.

break

default:
container = window
}

// cache starting position
start = location()

Expand Down