react轮播

支持滑动  左右移动  是否需要组件按钮 分页 回掉

import React,{Component,PropTypes} from ‘react‘;import {circleFunction} from ‘../main‘;import {method} from ‘../common‘;import BannerType from ‘./ButtonType‘import Pagination from ‘./Pagination‘class Banner extends Component{    static PropTypes={        menuData:PropTypes.Array,        autoPlay:PropTypes.Bool    };    constructor(props){        super(props);        circleFunction(this,[            ‘autoPlay‘,‘onClickLeft‘,‘onClickRight‘,‘onMouseLeave‘,‘onTouchStart‘,‘onTouchMove‘,‘onTouchEnd‘,            ‘onMouseDown‘,‘onMouseMove‘,‘onMouseUp‘,‘bind_event‘        ]);        this.state={            index:0,            indexAfter:null,            aniTime:0.5,        };        this.isRun = false;        this.isMouseEnter = false;        this.timer =null;        this.px = 0;        this.MoveX = 0;        this.startMove = 0;    };    componentDidMount(){        //install width for li        const liWid = this.refs.bannerHorizontal.offsetWidth;        this.setState({liWidth:liWid})    }    componentWillReceiveProps(nextProps){        if(‘menuData‘ in nextProps){            const data1 = method.clone(nextProps.menuData);            const data = nextProps.menuData;            data.unshift(data[data.length-1]);            data.push(data[1]);

this.setState({menuData:data,menuData1:data1},()=>{                if(‘autoPlay‘ in nextProps&&nextProps.autoPlay){                    this.autoPlay();                }            });        }    }    //autoPlay    autoPlay(){        const {index,menuData}= this.state;        const a = new Promise(resolve=>{            if(index>-(menuData.length-2)){                this.setState({index:index-1,aniTime:‘0.5‘})            }else if(index === - (menuData.length-2)) {                this.setState({index:0,aniTime:‘none‘},()=>{                    setTimeout(()=>{                        this.setState({index:-1,aniTime:‘0.5‘})                    },10);                })            }            resolve(index);            clearTimeout(this.timer);            this.timer = setTimeout(() => {                if(this.timer) {                    this.autoPlay()                }            }, 2000)        }).then(index=>{            const {callback} = this.props;            if(‘callback‘ in this.props){                index === -4 ? callback(0):callback(Math.abs(-index));            }        });

};    //卸载    componentWillUnmount(){        clearTimeout(this.timer);    }    //prev button    onClickRight(){        const {menuData,index}= this.state;        clearTimeout(this.timer);        if(!this.isRun){            this.isRun = true;            if(index>-(menuData.length-2)){                this.setState({index:index-1,aniTime:‘0.5‘})            }else if(index === - (menuData.length-2)) {                this.setState({index:0,aniTime:‘none‘},()=>{                    setTimeout(()=>{this.setState({index:-1,aniTime:‘0.5‘})},10);                })            }            const {callback} = this.props;            if(‘callback‘ in this.props){                index === -4 ? callback(0):callback(Math.abs(-index));            }            setTimeout(()=>this.isRun = false,500)        }    }    //next button    onClickLeft(){        const {menuData,index}= this.state;        clearTimeout(this.timer);        if(!this.isRun){            this.isRun = true;            if(index ===-1){                this.setState({index:(-(menuData.length-1)),aniTime:‘none‘},()=>{                    setTimeout(()=>{this.setState({index:-(menuData.length-2),aniTime:‘0.5‘})},10);                })            }else {                this.setState({index:index+1,aniTime:‘0.5‘})            }            const {callback} = this.props;            if(‘callback‘ in this.props){                index === -1 ? callback(menuData.length-3):callback(Math.abs(index+2));            }            setTimeout(()=>this.isRun = false,500)        }    }    //Pagination    callbackPag=(data)=>{        clearTimeout(this.timer);        const {callback} = this.props;        if(‘callback‘ in this.props){            callback(Math.abs(data));        }        this.setState({index:-(data+1)})    };    //leave to trigger    onMouseLeave(){        this.timer = setTimeout(() => {            if(this.timer&&this.props.autoPlay) {                this.autoPlay()            }        }, 5000)    };    //map img ·    mapList=(data)=>{        const {liWidth} = this.state;        return data.map((item,i)=>{            return <li style={{width:`${liWidth}px`}} key={i}>                <img alt={item.name} src={`./img/${item.url}`}/>            </li>        });    };    onTouchStart=(e)=>{        e.preventDefault();        clearTimeout(this.timer);        this.px = e.nativeEvent.changedTouches[0].pageX;    };    onTouchMove=(e)=>{        e.preventDefault();        clearTimeout(this.timer);        if(!this.isRun){            this.MoveX = e.nativeEvent.changedTouches[0].pageX - this.px;            this.refs.ul.style = `-webkit-transform:translateX(${this.state.liWidth*this.state.index+this.MoveX}px)`        }    };    onTouchEnd(e){        e.preventDefault();        this.bind_event();    };    onMouseDown(e){        e.preventDefault();        clearTimeout(this.timer);        this.px = e.nativeEvent.clientX;        this.isMouseEnter = true;    };    onMouseMove(e){        e.preventDefault();        clearTimeout(this.timer);        if(this.isMouseEnter){            this.MoveX = e.nativeEvent.clientX - this.px;            this.refs.ul.style = `-webkit-transform:translateX(${this.state.liWidth*this.state.index+this.MoveX}px)`        }    };    onMouseUp(e){        e.preventDefault();        this.isMouseEnter = false;        this.bind_event();    };    bind_event(){        const {menuData,index}= this.state;        const {callback} = this.props;        this.refs.ul.style =`transition:0.5s`;        if(this.MoveX<0&&this.MoveX>-100||this.MoveX>0&&this.MoveX<100){            this.refs.ul.style = `-webkit-transform:translateX(${this.state.liWidth*this.state.index}px)`        }else{            if(this.MoveX<-100){                if(index === - (menuData.length-2)) {                    this.setState({index:- (menuData.length-1),aniTime:‘0.5‘},()=>{                        setTimeout(()=>{                            this.setState({index:-1,aniTime:‘none‘})                        },450);                    })                }else{                    this.setState({index:index-1,aniTime:‘0.5‘})                }                if(‘callback‘ in this.props){                    callback(Math.abs(index));                }            }else if(this.MoveX>100){                if(index ===-1){                    this.setState({index:0,aniTime:‘0.5‘},()=>{                        setTimeout(()=>{                            this.setState({index:-(menuData.length-2),aniTime:‘none‘})                        },450);                    })                }else {                    this.setState({index:index+1,aniTime:‘0.5‘})                }                if(‘callback‘ in this.props){                    index === -1 ? callback(menuData.length-3):callback(Math.abs(index+2));                }            }        }    }    render(){        const {index,menuData,liWidth,aniTime,menuData1}=this.state;        const {showButton,showPagination} = this.props;        const ani = {            WebkitTransform:`translateX(${liWidth*index}px)`,            transition:`${aniTime}s`,        };        return (            <div                ref="bannerHorizontal"                className={`bannerHorizontal ${this.props.className}`}                style={{...this.props.style}}            >                <ul                    onTouchStart={this.onTouchStart}                    onTouchMove={this.onTouchMove}                    onTouchEnd={this.onTouchEnd}                    onMouseLeave={this.onMouseLeave}                    onMouseDown={this.onMouseDown}                    onMouseMove={this.onMouseMove}                    onMouseUp={this.onMouseUp}                    ref="ul" style={{transition:‘0.5s‘,...ani}}                    className="imgWap clearfix">                    {!!menuData&&this.mapList(menuData)}                </ul>                {this.props.children}                {showButton&&<BannerType {...this}/>}                {showPagination&&<Pagination index={-(index+1)} data={menuData1} {...this}/>}            </div>        )    }}export default Banner;
时间: 2024-08-04 05:51:30

react轮播的相关文章

React Native 之轮播图swiper组件

注释:swiper组件是第三方组件 所以在使用之前应该先在命令行安装,然后将第三方的模块引入(第三方模块地址:https://github.com/leecade/react-native-swiper) 安装完成后,我们需要完成轮播功能.因为可以到github看看swiper暴露的接口和参数.github地址是:https://github.com/leecade/react-native-swiper (1)引入swiper,前面也提到了require. var Swiper = requi

react 实现一个无限循环的轮播器 附github地址

一个简单的轮播 为了更具有通用和参考性,轮播组件中,轮播只使用了react,没有添加其他的状态管理,或者参数类型限制的库. github地址 最终效果 显示无限循环原理 如图所示,如果轮播里面有三个部分,那么可以在首端前添加一个跟最后一块一样的dom节点,同理在最末端添加跟首端相同的节点,这样当轮播到末端,在下一张的情况下,就可以无缝衔接首端的节点,然后当动画结束后,在直接切换到真正的首端,就实现了无缝衔接的轮播器 组件代码 import React, {Component} from 'rea

React Native布局实践:开发京东客户端首页(三)——轮播图的实现

上篇文章中,我们一起构建了京东客户端的TabBar,在本文中,将继续向大家介绍京东客户端首页轮播图及其下发功能按钮的开发方法,现在就让我们开始吧! 1.相关控件调研 目前在Github开源的轮播图控件,个人认为做得比较好的,一个是react-native-swiper(https://github.com/leecade/react-native-swiper),一个是react-native-viewpager(https://github.com/race604/react-native-v

基于Reactive Native轮播组件的应用开发以及移动端模拟器下的调试

总结下这段时间学习reactive native的一些东西,我们来认识一下,被炒得这么火的rn,究竟是个什么东西,以及如何去搭建自己的demo. reactive  native是什么 由facebook开发的一种应用框架,可以用react开发原生应用的框架.简单来说就是可以通过js和react来开发的一种框架. react是什么 一套js的框架,也是facebook开源.特点:jsx语法(类似XML),组件化模式,virtual DOM,单向数据流. 基本模式:每个react应用可视为组件的组

移动端Reactive Native轮播组件

移动端Reactive Native轮播组件 总结下这段时间学习reactive native的一些东西,我们来认识一下,被炒得这么火的rn,究竟是个什么东西,以及如何去搭建自己的demo. reactive  native是什么 由facebook开发的一种应用框架,可以用react开发原生应用的框架.简单来说就是可以通过js和react来开发的一种框架. react是什么 一套js的框架,也是facebook开源.特点:jsx语法(类似XML),组件化模式,virtual DOM,单向数据流

React-Native 实现无限轮播

1.配置环境 http://reactnative.cn/docs/0.25/getting-started.html 2.环境配置出现错误,请看这一篇:http://blog.csdn.net/p106786860/article/details/51052299 3.代码下载地址: https://github.com/BrisyIOS/React-Native-UnlimtedCarousel.git 4.需要将资源图片放到工程中. 5.代码展示如下: /** * Sample React

jQuery实现简易轮播图的效果

(图片素材取自于小米官网) 刚开始接触jQuery的学习,个人觉得如果为了实现多数的动态效果,jQuery的确很简易方便. 下面简易的轮播图效果,还请前辈多多指教~ (努力学习react vue angular中,加油~~~) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> &

小程序切换轮播图显示空白的问题

技术栈,taro编译的小程序,场景: 首页几个tab切换,默认第一个tab轮播图有数据,切换的时候后面的轮播图有的时候显示空白,请看: 首先父组件到子组件的值传过来了,自组件img src路径也有值,复制出来地址浏览器,显示没问题,路径问题排除 第一个tab三张轮播图,第二个两张轮播图,其他的tab都是一张,每个tab都从 数组形式传值过来的.有数据这是怎么回事,于是翻翻小郑许官网看到这 https://developers.weixin.qq.com/miniprogram/dev/compo

CSS3图片轮播效果

原文:CSS3图片轮播效果 在网页中用到图片轮播效果,单纯的隐藏.显示,那再简单不过了,要有动画效果,如果是自己写的话(不用jquery等),可能要费点时间.css3的出现,让动画变得不再是问题,而且简单易用.下面介绍我用css3与js写的一个图片轮播效果. 一般图片轮播就是三四张图片: <div class="wrap"> <div class="carousel"> <div><img src="http://