1
1
<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 >
5
7
</template >
6
8
7
9
<script >
@@ -12,78 +14,74 @@ export default {
12
14
readingTop: 0 ,
13
15
readingHeight: 1 ,
14
16
progressStyle: null ,
15
- transform: [' transform' ]
17
+ transform: undefined ,
18
+ running: false
16
19
}
17
20
},
18
21
watch: {
19
22
$readingShow () {
20
- this .base ()
23
+ this .progressStyle = this .getProgressStyle ()
24
+ this .$readingShow && window .addEventListener (' scroll' , this .base )
21
25
}
22
26
},
23
27
mounted () {
24
- this .base ()
28
+ this .transform = this .getTransform ()
29
+ this .progressStyle = this .getProgressStyle ()
30
+ this .$readingShow && window .addEventListener (' scroll' , this .base )
25
31
},
26
32
beforeDestroy () {
27
- this .$readingShow && window .removeEventListener (' scroll' , this .getReadingBase )
33
+ this .$readingShow && window .removeEventListener (' scroll' , this .base )
28
34
},
29
35
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 )
35
40
}
36
41
},
37
42
getReadingBase () {
38
43
this .readingHeight = this .getReadingHeight () - this .getScreenHeight ()
39
44
this .readingTop = this .getReadingTop ()
40
45
this .progressStyle = this .getProgressStyle ()
46
+ this .running = false
41
47
},
42
48
getReadingHeight () {
43
- return document .body .scrollHeight
44
- || document .body .offsetHeight
45
- || 0
49
+ return Math .max (document .body .scrollHeight , document .body .offsetHeight , 0 )
46
50
},
47
51
getScreenHeight () {
48
- return window .innerHeight
49
- || document .documentElement .clientHeight
50
- || 0
52
+ return Math .max (window .innerHeight , document .documentElement .clientHeight , 0 )
51
53
},
52
54
getReadingTop () {
53
- return window .pageYOffset
54
- || document .documentElement .scrollTop
55
- || 0
55
+ return Math .max (window .pageYOffset , document .documentElement .scrollTop , 0 )
56
56
},
57
57
getTransform () {
58
+ const div = document .createElement (' div' )
58
59
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
60
61
},
61
62
getProgressStyle () {
62
63
const progress = this .readingTop / this .readingHeight
63
64
switch (this .$readingShow ) {
64
65
case ' top' :
65
66
case ' bottom' :
66
- if (this .transform [ 0 ] ) {
67
- return ` ${ this .transform [ 0 ] } : scaleX(${ progress} )`
67
+ if (this .transform ) {
68
+ return ` ${ this .transform } : scaleX(${ progress} )`
68
69
} else {
69
70
return ` width: ${ progress * 100 } %`
70
71
}
71
72
break
72
73
case ' left' :
73
74
case ' right' :
74
- if (this .transform [ 0 ] ) {
75
- return ` ${ this .transform [ 0 ] } : scaleY(${ progress} )`
75
+ if (this .transform ) {
76
+ return ` ${ this .transform } : scaleY(${ progress} )`
76
77
} else {
77
78
return ` height: ${ progress * 100 } %`
78
79
}
79
80
break
80
81
default :
82
+ return null
81
83
break
82
84
}
83
- },
84
- supportCss (value ) {
85
- const div = document .createElement (' div' )
86
- return value in div .style
87
85
}
88
86
}
89
87
}
0 commit comments