Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@KBiswas98
Created January 25, 2020 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KBiswas98/46bcdef67e64a333060502cb05c3103e to your computer and use it in GitHub Desktop.
Save KBiswas98/46bcdef67e64a333060502cb05c3103e to your computer and use it in GitHub Desktop.
react-native slider/carousel using scrollView
componentDidMount() {
this._stopAutoPlay();
this._startAutoPlay();
}
componentWillUnmount() {
this._stopAutoPlay();
}
_goToNextPage = () => {
if (
ActivePortionOfScrollView >
Dimensions.get('window').width * (NumberOfSlide - 1)
)
ActivePortionOfScrollView = -Dimensions.get('window').width;
ActivePortionOfScrollView =
ActivePortionOfScrollView +
Dimensions.get('window').width +
AdjustingWidth;
this.refs.scrollview.scrollTo({
x: ActivePortionOfScrollView,
y: 0,
animated: true,
});
// console.log('ww' + ActivePortionOfScrollView);
};
_startAutoPlay = () => {
this._timerId = setInterval(this._goToNextPage, IntervalTime);
};
_stopAutoPlay = () => {
if (this._timerId) {
clearInterval(this._timerId);
this._timerId = null;
}
};
<View style={{marginTop: 10, marginBottom: 10}}>
<ScrollView
style={{}}
horizontal={true}
ref="scrollview"
onScroll={event => this.handleScroll(event)}>
<Image
source={require('../../../assets/images/one.jpg')}
style={styles.sliderItems}
/>
<Image
source={require('../../../assets/images/two.jpg')}
style={styles.sliderItems}
/>
<Image
source={require('../../../assets/images/three.jpg')}
style={styles.sliderItems}
/>
</ScrollView>
</View>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment