-
Notifications
You must be signed in to change notification settings - Fork 471
/
DemoCoverflow.js
145 lines (133 loc) · 3.13 KB
/
DemoCoverflow.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
import React from 'react';
import SwipeableViews from 'react-swipeable-views';
import Animated from 'animated/lib/targets/react-dom';
const styles = {
root: {
background: '#000',
padding: '0 50px',
},
slide: {
padding: '24px 16px',
color: '#fff',
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'column',
display: 'flex',
},
img: {
width: 180,
height: 180,
display: 'block',
marginBottom: 16,
},
};
const albums = [
{
name: 'Abbey Road',
src: '/static/album-art-1.jpg',
},
{
name: 'Bat Out of Hell',
src: '/static/album-art-2.jpg',
},
{
name: 'Homogenic',
src: '/static/album-art-3.jpg',
},
{
name: 'Number of the Beast',
src: '/static/album-art-4.jpg',
},
{
name: "It's Blitz",
src: '/static/album-art-5.jpg',
},
{
name: 'The Man-Machine',
src: '/static/album-art-6.jpg',
},
{
name: 'The Score',
src: '/static/album-art-7.jpg',
},
{
name: 'Lost Horizons',
src: '/static/album-art-8.jpg',
},
];
class DemoCoverflow extends React.Component {
state = {
index: 0,
position: new Animated.Value(0),
};
handleChangeIndex = index => {
this.setState({
index,
});
};
handleSwitch = (index, type) => {
if (type === 'end') {
Animated.timing(this.state.position, { toValue: index, duration: 150 }).start();
return;
}
this.state.position.setValue(index);
};
render() {
const { index, position } = this.state;
return (
<SwipeableViews
index={index}
style={styles.root}
onChangeIndex={this.handleChangeIndex}
onSwitching={this.handleSwitch}
>
{albums.map((album, currentIndex) => {
const inputRange = albums.map((_, i) => i);
const scaleOutputRange = inputRange.map(i => {
if (currentIndex === i) {
return 1;
}
return 0.7;
});
const scale = position.interpolate({
inputRange,
outputRange: scaleOutputRange,
});
const opacityOutputRange = inputRange.map(i => {
if (currentIndex === i) {
return 1;
}
return 0.3;
});
const opacity = position.interpolate({
inputRange,
outputRange: opacityOutputRange,
});
const translateOutputRange = inputRange.map(i => {
return 100 / 2 * (i - currentIndex);
});
const translateX = position.interpolate({
inputRange,
outputRange: translateOutputRange,
});
return (
<Animated.div
key={String(currentIndex)}
style={Object.assign(
{
opacity,
transform: [{ scale }, { translateX }],
},
styles.slide,
)}
>
<img style={styles.img} src={album.src} alt="cover" />
{album.name}
</Animated.div>
);
})}
</SwipeableViews>
);
}
}
export default DemoCoverflow;