Skip to content

Commit ba59161

Browse files
committed
polish: optimize by requestAnimationFrame
1 parent c422d81 commit ba59161

File tree

5 files changed

+38
-44
lines changed

5 files changed

+38
-44
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
[README](README.md) | [CHANGELOG](CHANGELOG.md)
44

5+
## 1.0.10
6+
7+
- optimize performance by requestAnimationFrame
8+
59
## 1.0.9
610

711
- fix bug about readingHeight
@@ -29,7 +33,6 @@ readingShow: // one of `top`, `bottom`, `left`, `right`, `true`, `false`
2933
---
3034
```
3135

32-
3336
## 1.0.4
3437

3538
- fix error on the 404 page

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ example
6161
### fixed
6262
- Type: `string`
6363
- Default: `top`
64+
- Required: `false`
6465

6566
support `top`, `bottom`, `left`, `right`
6667

@@ -106,7 +107,3 @@ $readingProgressImage = linear-gradient(-120deg, #E50743 0%, #F9870F 15%, #E8ED3
106107
## License
107108

108109
[MIT](http://opensource.org/licenses/MIT)
109-
110-
## Keywords
111-
112-
vue vuepress plugin reading-progress reading progress

ReadingProgress.vue

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<template>
2-
<div v-if="$readingShow" :class="$readingShow" class="reading-progress">
3-
<div :style="progressStyle" class="progress"></div>
4-
</div>
2+
<ClientOnly>
3+
<div v-if="$readingShow" :class="$readingShow" class="reading-progress">
4+
<div :style="progressStyle" class="progress"></div>
5+
</div>
6+
</ClientOnly>
57
</template>
68

79
<script>
@@ -12,78 +14,74 @@ export default {
1214
readingTop: 0,
1315
readingHeight: 1,
1416
progressStyle: null,
15-
transform: ['transform']
17+
transform: undefined,
18+
running: false
1619
}
1720
},
1821
watch: {
1922
$readingShow () {
20-
this.base()
23+
this.progressStyle = this.getProgressStyle()
24+
this.$readingShow && window.addEventListener('scroll', this.base)
2125
}
2226
},
2327
mounted () {
24-
this.base()
28+
this.transform = this.getTransform()
29+
this.progressStyle = this.getProgressStyle()
30+
this.$readingShow && window.addEventListener('scroll', this.base)
2531
},
2632
beforeDestroy () {
27-
this.$readingShow && window.removeEventListener('scroll', this.getReadingBase)
33+
this.$readingShow && window.removeEventListener('scroll', this.base)
2834
},
2935
methods: {
30-
base () {
31-
if (this.$readingShow) {
32-
this.transform = this.getTransform()
33-
this.progressStyle = this.getProgressStyle()
34-
window.addEventListener('scroll', this.getReadingBase, 200)
36+
base() {
37+
if (!this.running) {
38+
this.running = true
39+
requestAnimationFrame(this.getReadingBase)
3540
}
3641
},
3742
getReadingBase () {
3843
this.readingHeight = this.getReadingHeight() - this.getScreenHeight()
3944
this.readingTop = this.getReadingTop()
4045
this.progressStyle = this.getProgressStyle()
46+
this.running = false
4147
},
4248
getReadingHeight () {
43-
return document.body.scrollHeight
44-
|| document.body.offsetHeight
45-
|| 0
49+
return Math.max(document.body.scrollHeight, document.body.offsetHeight, 0)
4650
},
4751
getScreenHeight () {
48-
return window.innerHeight
49-
|| document.documentElement.clientHeight
50-
|| 0
52+
return Math.max(window.innerHeight, document.documentElement.clientHeight, 0)
5153
},
5254
getReadingTop () {
53-
return window.pageYOffset
54-
|| document.documentElement.scrollTop
55-
|| 0
55+
return Math.max(window.pageYOffset, document.documentElement.scrollTop, 0)
5656
},
5757
getTransform () {
58+
const div = document.createElement('div')
5859
const transformList = ['transform', '-webkit-transform', '-moz-transform', '-o-transform', '-ms-transform']
59-
return transformList.filter(item => this.supportCss(item))
60+
return transformList.find(item => item in div.style) || undefined
6061
},
6162
getProgressStyle () {
6263
const progress = this.readingTop / this.readingHeight
6364
switch (this.$readingShow) {
6465
case 'top':
6566
case 'bottom':
66-
if (this.transform[0]) {
67-
return `${this.transform[0]}: scaleX(${progress})`
67+
if (this.transform) {
68+
return `${this.transform}: scaleX(${progress})`
6869
} else {
6970
return `width: ${progress * 100}%`
7071
}
7172
break
7273
case 'left':
7374
case 'right':
74-
if (this.transform[0]) {
75-
return `${this.transform[0]}: scaleY(${progress})`
75+
if (this.transform) {
76+
return `${this.transform}: scaleY(${progress})`
7677
} else {
7778
return `height: ${progress * 100}%`
7879
}
7980
break
8081
default:
82+
return null
8183
break
8284
}
83-
},
84-
supportCss (value) {
85-
const div = document.createElement('div')
86-
return value in div.style
8785
}
8886
}
8987
}

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { path } = require('@vuepress/shared-utils')
1+
const path = require('path')
22

33
function checkRegularPath(regularPath, readingDir, fixed) {
44
if (readingDir === null) {

package.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "vuepress-plugin-reading-progress",
3-
"version": "1.0.9",
3+
"version": "1.0.10",
44
"main": "index.js",
5+
"type": "commonjs",
56
"description": "A reading progress bar plugin for VuePress",
67
"author": "tolking <qw13131wang@gmail.com>",
78
"license": "MIT",
@@ -10,17 +11,12 @@
1011
"url": "https://github.com/tolking/vuepress-plugin-reading-progress/issues"
1112
},
1213
"homepage": "https://github.com/tolking/vuepress-plugin-reading-progress",
13-
"doc": "https://github.com/tolking/vuepress-plugin-reading-progress",
14+
"doc": "https://github.com/tolking/vuepress-plugin-reading-progress#readme",
1415
"keywords": [
15-
"vue",
1616
"vuepress",
1717
"plugin",
1818
"reading-progress",
1919
"reading",
2020
"progress"
21-
],
22-
"devDependencies": {
23-
"@vuepress/shared-utils": "^1.0.0",
24-
"vuepress": "^1.0.0"
25-
}
21+
]
2622
}

0 commit comments

Comments
 (0)