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
Show file tree
Hide file tree
Changes from 2 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
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
```

Expand Down Expand Up @@ -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:
Expand Down
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.

17 changes: 13 additions & 4 deletions src/jump.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import easeInOutQuad from './easing'

export default class Jump {
jump(target, options = {}) {
this.start = window.pageYOffset

this.options = {
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'
Expand All @@ -19,6 +19,10 @@ export default class Jump {
? this.options.duration(this.distance)
: this.options.duration

!!this.options.container
? this.start = this.options.container.getBoundingClientRect().top
: this.start = window.pageYOffset

Choose a reason for hiding this comment

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

it seem not working, try:

    !!this.options.container
      ?  this.start = this.options.container.scrollTo
      :  this.start = window.pageYOffset

Copy link
Author

Choose a reason for hiding this comment

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

I adjusted the calculations and removed this part of code, now it should be much smoother. If you have trouble getting it to work please let me know and I'll try to help out.

requestAnimationFrame(time => this._loop(time))
}

Expand All @@ -30,15 +34,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
Expand Down