react-native-snap-carousel 轮播

效果如下:

9320442-4a222c0f793223d1.gif
6iIb4SR.gif

使用插件: https://www.npmjs.com/package/react-native-snap-carousel

基础使用方法:

 下载:  $ npm install --save react-native-snap-carousel
 引入:  import Carousel from 'react-native-snap-carousel'

 使用:
const horizontalMargin = 20;
const slideWidth = 280;
const itemWidth = slideWidth + horizontalMargin * 2;
const itemHeight = 200;
  export class MyCarousel extends Component {
      constructor(props) {
            super(props);
            this.state = {
                entries: [
                    { title: "安徒生童话" },
                    { title: "格林童话" },
                    { title: "我的童话"}
                ]
            }
       }


    _renderItem ({item, index}) {
        return (
            <View style={styles.slide}>
                <Text style={styles.title}>{ item.title }</Text>
            </View>
        );
    }
 
    render () {
        return (
            <Carousel
              ref={(c) => { this._carousel = c; }}
              data={this.state.entries}
              renderItem={this._renderItem}
              sliderWidth={sliderWidth}
              itemWidth={itemWidth}
            />
        );
    }
}


const styles = StyleSheet.create({ 
    slide: {
      borderRadius: 10
      backgroundColor: '#992211'
   }
})


常用参数

                 <Carousel 
                    ref={(c) => { this._carousel = c; }}  // 获取节点
                    inactiveSlideOpacity={1} // 不活动幻灯片的不透明度效果的值
                    inactiveSlideScale={0.7}  // 不活动幻灯片的缩放效果的值
                    firstItem={0} // 第一个显示Item
                    removeClippedSubviews={false} // 解决 IOS如果不滑动就不显示的额问题
                    data={this.state.cardList}
                    renderItem={this._renderItem}
                    sliderWidth={sliderWidth} // 轮播的宽度
                    itemWidth={itemWidth} //  单个Item的宽度
                    onSnapToItem={()=>{this.onSnapToItem()}}   //导航到项目时触发回
                    />

常用方法

获取当前显示Item的Index
const carouselCurrIndex = this._carousel.currentIndex;

跳转到指定Item

this._carousel.snapToItem(2) // 显示第二个item

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值