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

Added support for a container to scroll in #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Added support for a container to scroll in
mrenty committed Dec 10, 2015
commit 2e2426c3277cb7edc21576fab8e10e1e06d15664
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -86,7 +86,8 @@ Jump.jump('.selector', {
if(t < 1) return c / 2 * t * t + b
t--
return -c / 2 * (t * (t - 2) - 1) + b
}
},
container: '.selector'
})
```

@@ -144,6 +145,16 @@ Jump.jump('.selector', {
})
```

##### container

Container when you want to `jump()` inside an element.

```es6
Jump.jump('.selector', {
container: '.selector'
})
```

## Browser Support

Jump depends on the following browser APIs:
2 changes: 1 addition & 1 deletion dist/jump.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions src/jump.js
Original file line number Diff line number Diff line change
@@ -8,7 +8,8 @@ export default class Jump {
duration: options.duration,
offset: options.offset || 0,
callback: options.callback,
easing: options.easing || easeInOutQuad
easing: options.easing || easeInOutQuad,
container: document.querySelector(options.container) || null
}

this.distance = typeof target === 'string'
@@ -30,15 +31,20 @@ export default class Jump {
this.timeElapsed = time - this.timeStart
this.next = this.options.easing(this.timeElapsed, this.start, this.distance, this.duration)

window.scrollTo(0, this.next)
!!this.options.container
? this.options.container.scrollTop = this.next
: window.scrollTo(0, this.next)

this.timeElapsed < this.duration
? requestAnimationFrame(time => this._loop(time))
: this._end()
}

_end() {
window.scrollTo(0, this.start + this.distance)

!!this.options.container
? this.options.container.scrollTop = this.next + this.distance
: window.scrollTo(0, this.next + this.distance)

typeof this.options.callback === 'function' && this.options.callback()
this.timeStart = false