react-native 輪播圖 ——react-native-swiper使用
react-native-swiper的github地址
使用說明:
1. 先安裝React-native-swiper
<Swiper style={styles.swiper} height={200} horizontal={true} paginationStyle={{bottom: 10}} showsButtons={false}> <Image source={require('./js/img/a.jpg')} style={styles.img}/> <Image source={require('./js/img/b.jpg')} style={styles.img}/> <Image source={require('./js/img/c.jpg')} style={styles.img}/> </Swiper> const styles= StyleSheet.create({ swiper: {}, img: { width: dimensions.width, height: 200, } });
使用說明: 1. 先安裝React-native-swiper npm i react-native-swiper –save 2. 匯入Swiper import Swiper from ‘react-native-swiper’;
使用 Swiper
1 <Swiper 2 style={styles.swiper} //樣式 3height={200} //元件高度 4 loop={true} //如果設定為false,那麼滑動到最後一張時,再次滑動將不會滑到第一張圖片。 5 autoplay={true} //自動輪播 6 autoplayTimeout={4} //每隔4秒切換 7 horizontal={true} //水平方向,為false可設定為豎直方向 8 paginationStyle={{bottom: 10}} //小圓點的位置:距離底部10px9 showsButtons={false} //為false時不顯示控制按鈕 10 showsPagination={false} //為false不顯示下方圓點 11 dot={<View style={{ //未選中的圓點樣式 12 backgroundColor: 'rgba(0,0,0,.2)', 13 width: 18, 14 height: 18, 15 borderRadius: 4, 16 marginLeft: 10, 17 marginRight: 9, 18 marginTop: 9, 19 marginBottom: 9, 20 }}/>} 21 activeDot={<View style={{ //選中的圓點樣式 22 backgroundColor: '#007aff', 23 width: 18, 24 height: 18, 25 borderRadius: 4, 26 marginLeft: 10, 27 marginRight: 9, 28 marginTop: 9, 29 marginBottom: 9, 30 }}/>} 31 32 > 33 <Image source={require('./js/img/a.jpg')} style={styles.img}/> 34 <Image source={require('./js/img/b.jpg')} style={styles.img}/> 35 <Image source={require('./js/img/c.jpg')} style={styles.img}/> 36 </Swiper>
showsButtons | TRUE | bool | 為true時顯示控制按鈕 |
buttonWrapperStyle |
{backgroundColor: 'transparent', flexDirection: 'row', position: 'absolute', top: 0, left: 0, flex: 1, paddingHorizontal: 10, paddingVertical: 10, justifyContent: 'space-between', alignItems: 'center'} |
style | 自定義樣式 |
nextButton | <Text style={styles.buttonText}>›</Text> | element | 允許自定義下一個按鈕 |
prevButton | <Text style={styles.buttonText}>‹</Text> | element | 允許自定義上一個按鈕 |
屬性 | 預設值 | 型別 | 描述 |
showsPagination | TRUE | bool | 為true時顯示小圓點 |
paginationStyle | {...} | style | 允許自定義小圓點的樣式 |
renderPagination | - | function |
Complete control how to render pagination with three params (index, total, context) ref to this.state.index / this.state.total / this, For example: show numbers instead of dots. |
dot |
<View style={{backgroundColor:'rgba(0,0,0,.2)', width: 8, height: 8,borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3,}} /> |
element | 自定義沒有選中時的元件 |
activeDot |
<View style={{backgroundColor: '#007aff', width: 8, height: 8, borderRadius: 4, marginLeft: 3, marginRight: 3, marginTop: 3, marginBottom: 3,}} /> |
element | 自定義選中時的元件 |
dotStyle | - | object | 自定義選中的元素 |
dotColor | - | string | 自定義選中的元素 |
activeDotColor | - | string | 自定義選中的元素 |
activeDotStyle | - | object | 自定義選中的元素 |