-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadingProgress.vue
133 lines (130 loc) · 3.18 KB
/
ReadingProgress.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<template>
<ClientOnly>
<div v-if="$readingShow" :class="$readingShow" class="reading-progress">
<div :style="progressStyle" class="progress"></div>
</div>
</ClientOnly>
</template>
<script>
export default {
name: 'ReadingProgress',
data () {
return {
readingTop: 0,
readingHeight: 1,
progressStyle: null,
transform: undefined,
running: false
}
},
watch: {
$readingShow () {
this.progressStyle = this.getProgressStyle()
this.$readingShow && window.addEventListener('scroll', this.base)
}
},
mounted () {
this.transform = this.getTransform()
this.progressStyle = this.getProgressStyle()
this.$readingShow && window.addEventListener('scroll', this.base)
},
beforeDestroy () {
this.$readingShow && window.removeEventListener('scroll', this.base)
},
methods: {
base() {
if (!this.running) {
this.running = true
requestAnimationFrame(this.getReadingBase)
}
},
getReadingBase () {
this.readingHeight = this.getReadingHeight() - this.getScreenHeight()
this.readingTop = this.getReadingTop()
this.progressStyle = this.getProgressStyle()
this.running = false
},
getReadingHeight () {
return Math.max(document.body.scrollHeight, document.body.offsetHeight, 0)
},
getScreenHeight () {
return Math.max(window.innerHeight, document.documentElement.clientHeight, 0)
},
getReadingTop () {
return Math.max(window.pageYOffset, document.documentElement.scrollTop, 0)
},
getTransform () {
const div = document.createElement('div')
const transformList = ['transform', '-webkit-transform', '-moz-transform', '-o-transform', '-ms-transform']
return transformList.find(item => item in div.style) || undefined
},
getProgressStyle () {
const progress = this.readingTop / this.readingHeight
switch (this.$readingShow) {
case 'top':
case 'bottom':
if (this.transform) {
return `${this.transform}: scaleX(${progress})`
} else {
return `width: ${progress * 100}%`
}
break
case 'left':
case 'right':
if (this.transform) {
return `${this.transform}: scaleY(${progress})`
} else {
return `height: ${progress * 100}%`
}
break
default:
return null
break
}
}
}
}
</script>
<style lang="stylus" scoped>
$readingBgColor ?= transparent
$readingZIndex ?= 1000
$readingSize ?= 3px
$readingProgressColor ?= $accentColor
$readingProgressImage ?= none
.reading-progress
position fixed
z-index $readingZIndex
background $readingBgColor
overflow hidden
.progress
width 100%
height 100%
background $readingProgressColor
background-image $readingProgressImage
transform-origin 0% 0%
transition: transform .2s ease-out
.top
top 0
left 0
right 0
width 100%
height $readingSize
.bottom
bottom 0
left 0
right 0
width 100%
height $readingSize
.left
left 0
top 0
bottom 0
width $readingSize
height 100%
.right
right 0
top 0
bottom 0
width $readingSize
height 100%
</style>