Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit f066177

Browse files
committed
Change begin and end progress for target element
The following is for when there is a target element. Progress will begin once the top of the viewport hits the top of the target element. Progress will end once the bottom of the viewport hits the bottom of the target element.
1 parent 6603dd1 commit f066177

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/js/lib/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export default class ReadingProgress extends React.Component {
9494
measure() {
9595
this.targetHeight = this.targetEl.getBoundingClientRect().height
9696
this.viewportH = this.measureViewportHeight()
97-
this.max = this.targetHeight - this.viewportH + this.targetEl.offsetTop
97+
this.max = this.targetHeight - this.viewportH
9898
}
9999

100100
handleResize = () => {
@@ -106,11 +106,19 @@ export default class ReadingProgress extends React.Component {
106106
}
107107

108108
update = () => {
109-
const value = !this.props.rootEl ?
109+
let value = !this.props.rootEl ?
110110
window.pageYOffset || document.documentElement.scrollTop
111111
:
112112
this.rootEl.scrollTop
113113

114+
if (this.props.targetEl) {
115+
if (0 <= this.targetEl.getBoundingClientRect().top) {
116+
value = 0
117+
} else {
118+
value = value - (this.targetEl.getBoundingClientRect().top + window.pageYOffset)
119+
}
120+
}
121+
114122
this.setState({
115123
value,
116124
})

0 commit comments

Comments
 (0)