forked from oblador/react-native-parallax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParallaxImage.js
146 lines (133 loc) · 3.16 KB
/
ParallaxImage.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
/**
* @providesModule ParallaxImage
*/
'use strict';
var isEqual = require('lodash/lang/isEqual');
var React = require('react');
var {
View,
Image,
Animated,
StyleSheet,
Dimensions,
TouchableHighlight,
} = require('react-native');
var WINDOW_HEIGHT = Dimensions.get('window').height;
var ParallaxImage = React.createClass({
propTypes: {
onPress: React.PropTypes.func,
scrollY: React.PropTypes.object,
parallaxFactor: React.PropTypes.number,
imageStyle: Image.propTypes.style,
overlayStyle: View.propTypes.style,
},
getDefaultProps: function() {
return {
parallaxFactor: 0.2,
};
},
getInitialState: function() {
this.isLayoutStale = true;
return {
offset: 0,
height: 0,
width: 0,
};
},
setNativeProps: function(nativeProps) {
this._container.setNativeProps(nativeProps);
},
// Measure again since onLayout event won't pass the offset
handleLayout: function(event) {
if(this.isLayoutStale) {
(this._touchable || this._container).measure(this.handleMeasure);
}
},
componentWillReceiveProps: function(nextProps) {
if(!isEqual(nextProps, this.props)) {
this.isLayoutStale = true;
}
},
handleMeasure: function(ox, oy, width, height, px, py) {
this.isLayoutStale = false;
this.setState({
offset: py,
height,
width,
});
},
render: function() {
var { offset, width, height } = this.state;
var {
onPress,
scrollY,
parallaxFactor,
style,
imageStyle,
overlayStyle,
children,
...props
} = this.props;
var parallaxPadding = height * parallaxFactor;
var parallaxStyle = {
height: height + parallaxPadding * 2,
width: width,
};
if(scrollY) {
parallaxStyle.transform = [
{
translateY: scrollY.interpolate({
inputRange: [offset - height, offset + WINDOW_HEIGHT + height],
outputRange: [-parallaxPadding, parallaxPadding],
extrapolate: 'clamp',
}),
},
];
} else {
parallaxStyle.transform = [
{ translateY: -parallaxPadding },
];
}
var content = (
<View
ref={component => this._container = component}
style={[style, styles.container]}
onLayout={this.handleLayout}
>
<Animated.Image
{...props}
style={[imageStyle, parallaxStyle]}
pointerEvents="none"
/>
<View style={[styles.overlay, overlayStyle]}>
{children}
</View>
</View>
);
// Since we can't allow nested Parallax.Images, we supply this shorthand to wrap a touchable
// around the element
if(onPress) {
return (
<TouchableHighlight ref={component => this._touchable = component} onPress={onPress}>
{content}
</TouchableHighlight>
);
}
return content;
}
});
var styles = StyleSheet.create({
container: {
overflow: 'hidden',
position: 'relative',
},
overlay: {
flex: 1,
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
},
});
module.exports = ParallaxImage;