-
Notifications
You must be signed in to change notification settings - Fork 7
/
satellite-bg.js
156 lines (145 loc) · 3.84 KB
/
satellite-bg.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { isCurrnetIndexNextToIndex } from '../../utils/is-near-current'
import anime from 'animejs'
import EnhancedTransition from '../enhanced-transition'
import getDeviceType from '../../utils/get-device-type'
import map from 'lodash.map'
import mq from '../../utils/media-query'
import PropTypes from 'prop-types'
import React from 'react'
import styled from 'styled-components'
const _ = {
map,
}
const Wrapper = styled.div`
width: 100%;
height: 100%;
left: 0;
top: 0;
${mq.desktopAbove`
height: 852px;
width: 1440px;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
`}
position: absolute;
overflow: hidden;
`
const Image = styled.div`
opacity: 0;
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
${mq.mobileOnly`
background-image: url(${props => props.tabletImage});
background-size: auto 87%;
background-position: center bottom;
background-repeat: no-repeat;
`}
${mq.tabletOnly`
background-image: url(${props => props.tabletImage});
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
`}
${mq.desktopAbove`
background-image: url(${props => props.desktopImage});
background-size: auto auto;
background-position: center center;
background-repeat: no-repeat;
`}
`
class SatelliteBg extends React.Component {
static propTypes = {
currentIndex: PropTypes.number.isRequired,
animation: PropTypes.object.isRequired,
index: PropTypes.oneOfType([PropTypes.number, PropTypes.array]).isRequired,
image: PropTypes.object.isRequired,
}
constructor(props) {
super(props)
this._image = null
this.getImageDomNode = this._getImageDomNode.bind(this)
}
componentDidMount() {
const deviceType = getDeviceType()
this._registerAname(deviceType)
}
shouldComponentUpdate(nextProps) {
const { currentIndex, index } = nextProps
return isCurrnetIndexNextToIndex(currentIndex, index)
}
_registerAname(deviceType) {
const animation = this.props.animation[deviceType] || this.props.animation
this.onExitToNextAnime = anime({
...animation.exit,
targets: this._image,
autoplay: false,
})
this.enterFromPrevAnime = anime({
...animation.enter,
targets: this._image,
autoplay: false,
})
}
_getImageDomNode(ele) {
this._image = ele
}
_calTimeout(animationObj) {
const timeOfproterties = _.map(animationObj, prop => (prop.duration || 0) + (prop.delay || 0))
return Math.max(...timeOfproterties)
}
render() {
const {
currentIndex,
animation,
image,
index,
} = this.props
return (
<EnhancedTransition
currentIndex={currentIndex}
index={index}
onEnterFromPrev={() => {
if (this.enterFromPrevAnime) {
this.enterFromPrevAnime.restart()
this.enterFromPrevAnime.play()
}
}}
onEnterFromNext={() => {
if (this.onExitToNextAnime) {
this.onExitToNextAnime.reverse()
this.onExitToNextAnime.play()
}
}}
onExitToPrev={() => {
if (this.enterFromPrevAnime) {
this.enterFromPrevAnime.reverse()
this.enterFromPrevAnime.play()
}
}}
onExitToNext={() => {
if (this.onExitToNextAnime) {
this.onExitToNextAnime.restart()
this.onExitToNextAnime.play()
}
}}
timeout={{
enter: this._calTimeout(animation.enter),
exit: this._calTimeout(animation.exit),
}}
>
<Wrapper>
<Image
innerRef={this.getImageDomNode}
desktopImage={image.desktop.url}
tabletImage={image.tablet.url}
/>
</Wrapper>
</EnhancedTransition>
)
}
}
export default SatelliteBg