Skip to content

Commit f81f0a8

Browse files
committed
feat: support scroll animation
1 parent f24e5c9 commit f81f0a8

File tree

6 files changed

+1096
-45
lines changed

6 files changed

+1096
-45
lines changed

docs-src/src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import Vue from 'vue'
22
import demo from './demo.vue'
3-
import scrollSpy from '../../src/index'
3+
import scrollSpy, { Easing } from '../../src/index'
44

5-
Vue.use(scrollSpy)
5+
Vue.use(scrollSpy, {
6+
easing: Easing.Cubic.In
7+
})
68

79
new Vue({ // eslint-disable-line no-new
810
el: '#demo',

docs/app.js

Lines changed: 1039 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@
3737
"eslint-plugin-promise": "^3.6.0",
3838
"eslint-plugin-standard": "^3.0.1",
3939
"mkdirp": "^0.5.1"
40+
},
41+
"dependencies": {
42+
"@tweenjs/tween.js": "^17.2.0"
4043
}
4144
}

src/animate.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import TWEEN from '@tweenjs/tween.js'
2+
3+
const requestAnimationFrame = (function () {
4+
return window.requestAnimationFrame ||
5+
window.webkitRequestAnimationFrame ||
6+
window.mozRequestAnimationFrame ||
7+
window.oRequestAnimationFrame ||
8+
window.msRequestAnimationFrame ||
9+
function (callback) {
10+
window.setTimeout(callback, 1000 / 60)
11+
}
12+
})()
13+
14+
function animate () {
15+
if (TWEEN.update()) {
16+
requestAnimationFrame(animate)
17+
}
18+
}
19+
20+
requestAnimationFrame(animate)
21+
22+
export const Easing = TWEEN.Easing
23+
24+
export function scrollWithAnimation (scrollEl, current, target, time, easing) {
25+
new TWEEN.Tween({ postion: current })
26+
.to({ postion: target }, time)
27+
.easing(easing)
28+
.onUpdate(function (val) {
29+
scrollEl.scrollTop = val.postion
30+
})
31+
.start()
32+
33+
animate()
34+
}

src/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { scrollWithAnimation, Easing } from './animate.js'
2+
13
export default function install (Vue, options) {
24
const bodyScrollEl = {}
35

@@ -35,8 +37,9 @@ export default function install (Vue, options) {
3537
allowNoActive: false,
3638
data: null,
3739
offset: 0,
38-
time: 200,
40+
time: 500,
3941
steps: 30,
42+
easing: null,
4043
active: {
4144
selector: null,
4245
class: 'active'
@@ -114,6 +117,11 @@ export default function install (Vue, options) {
114117

115118
if (idScrollSections[index]) {
116119
const target = getOffsetTop(idScrollSections[index]) - options.offset
120+
if (options.easing) {
121+
scrollWithAnimation(scrollEl, current, target, options.time, options.easing)
122+
return
123+
}
124+
117125
const time = options.time
118126
const steps = options.steps
119127
const timems = parseInt(time / steps)
@@ -287,3 +295,5 @@ export default function install (Vue, options) {
287295
}
288296
})
289297
}
298+
299+
export { Easing }

0 commit comments

Comments
 (0)