-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFullPageScrollToMove.js
80 lines (67 loc) · 2.04 KB
/
FullPageScrollToMove.js
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
const FullPageScrollToMove = ({ turn_on = true, html }) => {
const BoxRef = useRef(null)
const scroll_y = useContext(WindowScrollYContext)
const [can_render_comp, set_can_render_comp] = useState(false)
useEffect(() => {
initial_moving_board(BoxRef)
add_func_to_img(BoxRef)
set_can_render_comp(true)
}, [])
const comp_list = Array.from(
document.getElementsByClassName("smc")
)
if (!turn_on || window.innerWidth < 1024) {
return (
<>
<div dangerouslySetInnerHTML={{ __html: html }} />
{can_render_comp && comp_list.map((comp, index) => (
<CompScrollToMoveConnector full_page={turn_on} key={`key-${index + 1}`} target_DOM={comp} />
))}
</>
)
}
return (
<>
<div
ref={BoxRef}
style={{
transform: `translateY(${-scroll_y}px)`
}}
dangerouslySetInnerHTML={{ __html: html }}
/>
{can_render_comp && comp_list.map((comp, index) => (
<CompScrollToMoveConnector full_page={turn_on} key={`key-${index + 1}`} target_DOM={comp} />
))}
</>
)
}
const initial_moving_board = BoxRef => {
if (!BoxRef.current) return
document.body.style.height = `${BoxRef.current.clientHeight}px`
BoxRef.current.style.position = "fixed"
BoxRef.current.style.top = 0
BoxRef.current.style.left = 0
BoxRef.current.style.transition = `transform .7s ease-out`
BoxRef.current.style.width = "100%"
}
const add_func_to_img = BoxRef => {
if (!BoxRef.current) return
const img_list = Array.from(document.getElementsByTagName('img'))
img_list.forEach((img) => {
img.addEventListener('load', set_MB_height_to_body(BoxRef))
})
}
const set_MB_height_to_body = BoxRef => () => {
document.body.style.height = `${BoxRef.current.clientHeight}px`
}
import React, {
useEffect,
forwardRef,
useRef,
useContext,
useState
} from 'react'
import ReactDOM from 'react-dom'
import { WindowScrollYContext } from './DetectScrollY/DetectWindowScrollY'
import CompScrollToMoveConnector from './CompScrollToMove'
export default FullPageScrollToMove