@@ -32,12 +32,14 @@ export function usePercentageSeen(element: HTMLElement | null, onChange: (percen
3232 const overTopEdge = elementOffsetTop < topEdge
3333
3434 // calculate the percentage on screen
35- // NOTE could divide by min(elementHeight, viewportHeight) instead .
35+ // NOTE we divide by min(elementHeight, viewportHeight).
3636 // That way, if element is larger than screen, will return 100% when element covers entire screen
3737 const calcPercentage =
38- ( overTopEdge ? elementOffsetTop + elementHeight - topEdge : bottomEdge - elementOffsetTop ) / elementHeight
38+ ( overTopEdge ? elementOffsetTop + elementHeight - topEdge : bottomEdge - elementOffsetTop ) /
39+ Math . min ( elementHeight , viewportHeight )
3940 // Restrict the range to between 0 and 1
40- const percentage = Math . round ( clampValue ( 0 , 1 , calcPercentage ) * 100 ) / 100
41+ const percentage = clampValue ( 0 , 1 , calcPercentage )
42+
4143 onChange ( percentage )
4244 } )
4345
@@ -67,7 +69,7 @@ export function useYPercentageOnScreen(
6769 const trigger = elementPos - viewportHeight
6870 // now calculate the element's position y on screen, and get percentage y
6971 // also restrict range to between 0 and 1
70- const percentage = Math . round ( clampValue ( 0 , 1 , ( scrollPos - trigger ) / viewportHeight ) * 100 ) / 100
72+ const percentage = clampValue ( 0 , 1 , ( scrollPos - trigger ) / viewportHeight )
7173 onChange ( percentage )
7274 } )
7375
@@ -80,11 +82,8 @@ export function useScrollPosition(onChange: ({ yPos, yPercentage }: { yPos: numb
8082 scrollHooks . push ( ( ) => {
8183 const yPos = window . scrollY
8284 const yPercentage =
83- Math . round (
84- ( document . documentElement . scrollTop /
85- ( document . documentElement . scrollHeight - document . documentElement . clientHeight ) ) *
86- 100
87- ) / 100
85+ document . documentElement . scrollTop /
86+ ( document . documentElement . scrollHeight - document . documentElement . clientHeight )
8887 onChange ( { yPos, yPercentage } )
8988 } )
9089
0 commit comments