Repository navigation
Expand file tree
/
Copy pathCompScrollToMove.js
More file actions
116 lines (92 loc) · 2.8 KB
/
Copy pathCompScrollToMove.js
File metadata and controls
116 lines (92 loc) · 2.8 KB
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
class CompScrollToMove extends React.Component {
componentDidMount() {
this.props.handle_scroll()
}
componentDidUpdate(prevProps) {
const { scroll_y, handle_scroll, move_smc } = this.props
if (prevProps.scroll_y !== scroll_y) {
handle_scroll()
}
move_smc()
}
render () {
return null
}
}
const stateBox1 = withState('position_y', 'set_position_y', 0)
const stateBox2 = withState('is_show', 'set_is_show', false)
const stateBox3 = withState('y_from_show', 'set_y_from_show', 0)
const logicBox1 = withHandlers({
handle_scroll: ({
set_position_y,
handle_is_show,
target_DOM
}) => () => {
set_position_y(get_position(target_DOM))
handle_is_show()
},
move_smc: ({target_DOM, full_page, y_from_show}) => () => {
let transform_data = {}
const param_arr = ['x', 'y', 'z', 'rotationX', 'rotationY', 'rotationZ', 'skewX', 'skewY']
param_arr.forEach((name) => {
const data_str = `smc${name.replace(name[0], name[0].toUpperCase())}`
if (!target_DOM.dataset[data_str]) return
transform_data[name] = Number(target_DOM.dataset[data_str]) * (-1) * y_from_show
})
const is_mobile = window.innerWidth <= 1024
const transition_time = (is_mobile || !full_page) ? 0 : 0.7
TweenMax.to(target_DOM, transition_time, {
...transform_data
})
}
})
const logicBox2 = withHandlers({
handle_is_show: ({target_DOM, set_is_show, set_y_from_show, is_show, scroll_y}) => () => {
if (!target_DOM) return
if (
scroll_y + window.innerHeight >=
get_position(target_DOM) &&
!is_show
) {
set_is_show(true)
} else if (
scroll_y + window.innerHeight < get_position(target_DOM) &&
is_show
) {
set_is_show(false)
}
set_y_from_show(
scroll_y + window.innerHeight - get_position(target_DOM) > 0 ?
scroll_y + window.innerHeight - get_position(target_DOM) :
0
)
},
})
const get_position = element => {
let xPosition = 0
let yPosition = 0
while (element) {
xPosition +=
element.offsetLeft - element.scrollLeft + element.clientLeft
yPosition += element.offsetTop - element.scrollTop + element.clientTop
element = element.offsetParent
}
return yPosition
}
const CompScrollToMoveConnectorBase = compose(
stateBox1,
stateBox2,
stateBox3,
logicBox2,
logicBox1
)(CompScrollToMove)
const CompScrollToMoveConnector = props => {
const scroll_y = useContext(WindowScrollYContext)
return <CompScrollToMoveConnectorBase {...props} scroll_y={scroll_y} />
}
import React, { useContext } from "react"
import ReactDOM from 'react-dom'
import { compose, withHandlers, withState } from 'recompose'
import { TweenMax } from 'gsap'
import { WindowScrollYContext } from './DetectScrollY/DetectWindowScrollY'
export default CompScrollToMoveConnector