Created
May 5, 2017 05:06
-
-
Save heaversm/671b531164f4fc41f43fd691f4e7efdf to your computer and use it in GitHub Desktop.
React Native Custom Carousel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import { Animated, View, StyleSheet, Image, Dimensions, ScrollView } from 'react-native' | |
const deviceWidth = Dimensions.get('window').width | |
const FIXED_BAR_WIDTH = 280 | |
const BAR_SPACE = 10 | |
const images = [ | |
'https://s-media-cache-ak0.pinimg.com/originals/ee/51/39/ee5139157407967591081ee04723259a.png', | |
'https://s-media-cache-ak0.pinimg.com/originals/40/4f/83/404f83e93175630e77bc29b3fe727cbe.jpg', | |
'https://s-media-cache-ak0.pinimg.com/originals/8d/1a/da/8d1adab145a2d606c85e339873b9bb0e.jpg', | |
] | |
export default class App extends Component { | |
numItems = images.length | |
itemWidth = (FIXED_BAR_WIDTH / this.numItems) - ((this.numItems - 1) * BAR_SPACE) | |
animVal = new Animated.Value(0) | |
render() { | |
let imageArray = [] | |
let barArray = [] | |
images.forEach((image, i) => { | |
console.log(image, i) | |
const thisImage = ( | |
<Image | |
key={`image${i}`} | |
source={{uri: image}} | |
style={{ width: deviceWidth }} | |
/> | |
) | |
imageArray.push(thisImage) | |
const scrollBarVal = this.animVal.interpolate({ | |
inputRange: [deviceWidth * (i - 1), deviceWidth * (i + 1)], | |
outputRange: [-this.itemWidth, this.itemWidth], | |
extrapolate: 'clamp', | |
}) | |
const thisBar = ( | |
<View | |
key={`bar${i}`} | |
style={[ | |
styles.track, | |
{ | |
width: this.itemWidth, | |
marginLeft: i === 0 ? 0 : BAR_SPACE, | |
}, | |
]} | |
> | |
<Animated.View | |
style={[ | |
styles.bar, | |
{ | |
width: this.itemWidth, | |
transform: [ | |
{ translateX: scrollBarVal }, | |
], | |
}, | |
]} | |
/> | |
</View> | |
) | |
barArray.push(thisBar) | |
}) | |
return ( | |
<View | |
style={styles.container} | |
flex={1} | |
> | |
<ScrollView | |
horizontal | |
showsHorizontalScrollIndicator={false} | |
scrollEventThrottle={10} | |
pagingEnabled | |
onScroll={ | |
Animated.event( | |
[{ nativeEvent: { contentOffset: { x: this.animVal } } }] | |
) | |
} | |
> | |
{imageArray} | |
</ScrollView> | |
<View | |
style={styles.barContainer} | |
> | |
{barArray} | |
</View> | |
</View> | |
) | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
alignItems: 'center', | |
justifyContent: 'center', | |
}, | |
barContainer: { | |
position: 'absolute', | |
zIndex: 2, | |
top: 40, | |
flexDirection: 'row', | |
}, | |
track: { | |
backgroundColor: '#ccc', | |
overflow: 'hidden', | |
height: 2, | |
}, | |
bar: { | |
backgroundColor: '#5294d6', | |
height: 2, | |
position: 'absolute', | |
left: 0, | |
top: 0, | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
How can i adapt it to be rtl?
Thank you