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}
0 commit comments