美文网首页
用react实现的一个轮播图

用react实现的一个轮播图

作者: 风间澈618 | 来源:发表于2017-12-28 19:06 被阅读0次

最近在写react,实现了一个轮播图功能,才对react组件和状态更多一点的了解。
实现的功能是图片自动轮播,当鼠标移入图片时停止轮播,点击左右箭头进行平滑移动,底部小圆点进行标识,轮播图随屏幕大小变化。具体是这样一个结构。


m.png
class Slide extends React.Component {
    constructor(...args) {
        super(...args)
        this.state = {
            slideUrl: [],
            index: 0,
            liWidth: document.body.clientWidth,
            posLeft: 0
        }
    }
 /*
重置图片宽度
*/
    onWindowResize() {
        let liWidth2 = document.body.clientWidth;
        this.setState({
            liWidth: liWidth2
        })
    }
    componentDidMount() {
        this.setState({
            slideUrl: [
                {
                    'imageUrl': img1
                },
                {
                    'imageUrl': img2
                },
                {
                    'imageUrl': img3
                }
                , {
                    'imageUrl': img4
                },
                {
                    'imageUrl': img5
                }
            ],
            index: 0,
        })
        window.addEventListener('resize', this.onWindowResize.bind(this));
        this.tick();
    }
    /**
    * 组件即将卸载时调用。一般在componentDidMount注册的监听事件需要在该方法中注销删除,以免不必要的错误.
    * @return void
    */
    componentWillUnmount() {
        //在组件即将销毁的时候取消resize的监听
        window.removeEventListener('resize', this.onWindowResize.bind(this));
        this._isMounted = false;
        clearInterval(this.play)

    }
/*轮播的功能*/
    turn(direction) {
        var index = this.state.index;
        this._isMounted = true;
        if (direction === 'left') {
            index++
            index = index >= this.state.slideUrl.length ? 0 : index;
        } else if (direction === 'right') {
            index--
            index = index < 0 ? this.state.slideUrl.length - 1 : index;
        }
        if (this._isMounted) {
            this.setState({
                posLeft: -this.state.liWidth * (index),
                index: index
            }, () => {
                this.state.index = index
            })
        }
    }
/*定时器*/
    tick() {
        this.play = setInterval(() => {
            this.turn('left');
        }, 1000)
    }
    over() {
        clearInterval(this.play)
    }
    render() {
        const bannerList = this.state.slideUrl;
        let arr = [];
        for (let i = 0; i < bannerList.length; i++) {
            arr[i] = <span className= {this.state.index === i ? 'circle active' : 'circle'} key={i}></span>
        }
       
        return (<div className="banner"
            style={{
                width: this.state.liWidth
            }}
            onMouseOver={this.over.bind(this)}
            onMouseOut={this.tick.bind(this)}
            >
               <ul className='bannerUl' style={{
                'left': this.state.posLeft,
                width: this.state.liWidth * this.state.slideUrl.length,
                Transition: 'all 0.4s',
                WebkitTransition: 'all 0.4s', // note the capital 'W' here
                msTransition: 'all 0.4s' // 'ms' is the only lowercase vendor prefix
            }}>
                  {
            bannerList.map((value, key) => {
                return (<li key={key} style={{
                        width: this.state.liWidth
                    }}><img src={value.imageUrl}/></li>)
            })
            }
               </ul>
               <div  className="ban_btn">
                    <span onClick={this.turn.bind(this, 'left')}>左</span>
                    <span onClick={this.turn.bind(this, 'right')}>右</span>
               </div>
               <div className='circles'>
               {arr }
            </div>
           </div>
        )
    }
}
export default Slide;

小提示

1·如果不在render()中使用某些东西,它就不应该在状态中
2·它将使用 this.setState() 来更新组件局部状态
3·构造函数是唯一能够初始化 this.state 的地方。
4因为 this.props 和 this.state 可能是异步更新的,你不应该依靠它们的值来计算下一个状态。
5如果列表可以重新排序,我们不建议使用索引来进行排序,因为这会导致渲染变得很慢。如果你想要了解更多
6 行内的style样式命名是驼峰式命名

相关文章

  • 【开源库】Github 标🌟6k+,React Native开发

    轮播图大家都比较熟悉 ,React Native 如何实现一个轮播图呢?官方是没有提供轮播图组件的,但大家一定用过...

  • 用react实现的一个轮播图

    最近在写react,实现了一个轮播图功能,才对react组件和状态更多一点的了解。实现的功能是图片自动轮播,当鼠标...

  • RN Banner轮播图的实现

    React Native 的ScrollView组件实现轮播图.定时任务实现滚动,TouchableWithout...

  • 项目-轮播图

    整个轮播图分为三部分:轮播指标、轮播项目及轮播导航。用boostrap实现轮播图要比用js、jQuery方便的多,...

  • 用js原生实现轮播图

    用jquery实现轮播图非常简单的啦!有没有想过用原生js实现轮播图呢???今天琢磨了一下,摸索出来一个,就和大家...

  • React Native 学习之ScrollView:自动轮播图

    Demo展示 Github上有两个实现轮播图的控件,一个是React-native-swiper,一个是react...

  • Slider - 轮播图

    简介: 用react开发的轮播图组件,支持淡入淡出、水平滚动、垂直滚动的无缝轮播效果。可自定义轮播内容。 API ...

  • React轮播-----react-bootstrap

    React项目 使用轮播图 React-bootstrap中的轮播(Carousel)模块参考github: ht...

  • ReactNative实现轮播图

    先看App运行效果: react-native中有专门实现轮播图的模块Swiper,和引入React的方式一样,通...

  • ReactJs写旋转木马轮播图

    备注:最近工作需要,要用react实现旋转木马的轮播图效果,在网上查了查没有相似的案例,只有用react实现的简单...

网友评论

      本文标题:用react实现的一个轮播图

      本文链接:https://www.haomeiwen.com/subject/nrwvgxtx.html