Skip to content

Commit

Permalink
feat: support scroll animation
Browse files Browse the repository at this point in the history
  • Loading branch information
ibufu committed Apr 16, 2018
1 parent f24e5c9 commit f81f0a8
Show file tree
Hide file tree
Showing 6 changed files with 1,096 additions and 45 deletions.
6 changes: 4 additions & 2 deletions docs-src/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Vue from 'vue'
import demo from './demo.vue'
import scrollSpy from '../../src/index'
import scrollSpy, { Easing } from '../../src/index'

Vue.use(scrollSpy)
Vue.use(scrollSpy, {
easing: Easing.Cubic.In
})

new Vue({ // eslint-disable-line no-new
el: '#demo',
Expand Down
1,081 changes: 1,039 additions & 42 deletions docs/app.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"mkdirp": "^0.5.1"
},
"dependencies": {
"@tweenjs/tween.js": "^17.2.0"
}
}
34 changes: 34 additions & 0 deletions src/animate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import TWEEN from '@tweenjs/tween.js'

const requestAnimationFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60)
}
})()

function animate () {
if (TWEEN.update()) {
requestAnimationFrame(animate)
}
}

requestAnimationFrame(animate)

export const Easing = TWEEN.Easing

export function scrollWithAnimation (scrollEl, current, target, time, easing) {
new TWEEN.Tween({ postion: current })
.to({ postion: target }, time)
.easing(easing)
.onUpdate(function (val) {
scrollEl.scrollTop = val.postion
})
.start()

animate()
}
12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { scrollWithAnimation, Easing } from './animate.js'

export default function install (Vue, options) {
const bodyScrollEl = {}

Expand Down Expand Up @@ -35,8 +37,9 @@ export default function install (Vue, options) {
allowNoActive: false,
data: null,
offset: 0,
time: 200,
time: 500,
steps: 30,
easing: null,
active: {
selector: null,
class: 'active'
Expand Down Expand Up @@ -114,6 +117,11 @@ export default function install (Vue, options) {

if (idScrollSections[index]) {
const target = getOffsetTop(idScrollSections[index]) - options.offset
if (options.easing) {
scrollWithAnimation(scrollEl, current, target, options.time, options.easing)
return
}

const time = options.time
const steps = options.steps
const timems = parseInt(time / steps)
Expand Down Expand Up @@ -287,3 +295,5 @@ export default function install (Vue, options) {
}
})
}

export { Easing }

0 comments on commit f81f0a8

Please sign in to comment.