import React from 'react';
import PropTypes from 'prop-types'; import {getSwipeWay} from '../utils/swipe'; //imgs : 图片src数组
//playTime : 轮播下一张图片的时间间隔
//notAuto : 是否开启自动轮播 默认开启 false const PropsChecker = {
imgs : PropTypes.array.isRequired,
playTime : PropTypes.number,
notAuto : PropTypes.bool,
}; class Carousel extends React.Component {
static defaultProps = {
playTime : 5000,
notAuto : false,
} constructor(args){
super(args);
this.state = {
};
//缓存各个currIndex的ul之后的标签
this.storeElements = {};
//判断滑动手势
this.swipeWay = getSwipeWay(50);//闸值50
//图片显示的限制
this.limit = 3;
//当前展示的图片
this.currIndex = 0;
//展示的数组
this.showImgs = [];
//手势滑动坐标
this.position = {
x1:0,
x2:0,
y1:0,
y2:0,
};
//<ul>
this.Ul = null;
//禁止在transiton的期间操作
this.isTransition = false;
//定时器
this.timer = 0;
} componentDidMount(){
this.autoPlay();
} componentWillUnmount(){
clearTimeout(this.timer);
} getHead(arr){
if(Array.isArray(arr)){
return arr[0];
}
console.error('非数组');
} getLast(arr){
if(Array.isArray(arr)){
const len = arr.length;
return arr[len-1];
}
console.error('非数组');
} calcIndex(){
const {imgs} = this.props;
const len = imgs.length;
const limit = this.limit;
const currIndex = this.currIndex; if(currIndex == 0){
this.showImgs = imgs.slice(0,limit - 1);
this.showImgs.unshift(this.getLast(imgs));
return;
} if(currIndex == len - 1){
this.showImgs = imgs.slice(len -2 ,len);
this.showImgs.push(this.getHead(imgs));
return;
} this.showImgs = imgs.slice(currIndex -1 , currIndex + limit -1); } changeCurrIndex(flag){
const {imgs} = this.props;
const last = imgs.length -1;
const currIndex = this.currIndex;
if(flag === '-'){
this.currIndex = currIndex == 0 ? last : currIndex -1 ;
return;
} if(flag === '+'){
this.currIndex = currIndex == last ? 0 : currIndex + 1 ;
return;
}
} ulTranslate(value){
const Ul = this.Ul;
if(Ul){ if(Ul.style.webkitTranslate ){
Ul.style.webkitTranslate = value;
}else{
Ul.style.translate = value;
} } } createUl(){
const currIndex = this.currIndex;
const storeElements = this.storeElements; //缓存这些标签,避免多次创建,很有必要
if(!storeElements[currIndex]){
//要保证<ul>key不同 也就是每次轮播后都要是新的标签,有损性能
const Ul = (<ul onTouchEnd={this.touchEnd}
onTouchMove={this.touchMove}
onTouchStart={this.touchStart}
onTransitionEnd={this.transitionEnd} ref={(ele)=>this.Ul=ele} key={currIndex}> {this.createLis()}
</ul>); storeElements[currIndex] = Ul;
} return storeElements[currIndex];
} createLis(){
this.calcIndex();
const imgs = this.showImgs; return imgs.map((src,i)=>{
const liStyle = {
// translate:(-i)+'00%',
translate:( (i+'00') - 100 ) + '%',
WebkitTranslate:( (i+'00') - 100 ) + '%',
}; return <li className='item' key={i} style={liStyle} ><img src={src} /></li>;
}); } touchStart = (e) => {
if(this.isTransition){
return;
} clearTimeout(this.timer); const {clientX,clientY} = e.touches[0];
this.position.x1 = clientX;
this.position.y1 = clientY;
} touchMove = (e) => { } touchEnd = (e) => {
if(this.isTransition){
return;
} const {clientX,clientY} = e.changedTouches[0];
this.position.x2 = clientX;
this.position.y2 = clientY; const {x1,x2,y1,y2} = this.position; const direction = this.swipeWay(x1,x2,y1,y2); if( direction === 'Left'){
this.changeCurrIndex('+');
this.isTransition = true;
this.ulTranslate('-100%');
} if(direction === 'Right'){
this.changeCurrIndex('-');
this.isTransition = true;
this.ulTranslate('100%');
}
} next(){
this.changeCurrIndex('+');
this.isTransition = true;
this.ulTranslate('-100%');
} autoPlay(){
const {playTime,notAuto} = this.props;
if(!notAuto){
this.timer = setTimeout(()=>{
this.next();
},playTime);
}
} transitionEnd = (e) => {
this.forceUpdate(()=>{
this.isTransition = false;
this.autoPlay();
});
} render(){ return (<div className='mm-carousel' >
{this.createUl()}
</div>);
}
} export default Carousel; Carousel.propTypes = PropsChecker;

继上一篇随笔,优化3张以上图片轮播React组件的更多相关文章

  1. VS2010/VS2013项目创建及通过ADO.NET连接mysql/sql server步骤(VS2013连接成功步骤见上一篇随笔)

    本随笔主要是对初学者通过ADO.NET连接数据库的步骤(刚开始我也诸多不顺,所以总结下,让初学者熟悉步骤) 1.打开VS新建一个项目(这里的VS版本不限,建项目都是一样的步骤) VS2010版本如图: ...

  2. iOS开发UI篇—UIScrollView控件实现图片轮播

    iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: #import "YYV ...

  3. 【转】 iOS开发UI篇—UIScrollView控件实现图片轮播

    原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 ...

  4. 使用Ajax+jQuery来实现前端收到的数据在console上显示+简单的主页设计与bootstrap插件实现图片轮播

    1.实现前端输入的数据在console上显示 上一篇是解决了在前端的输入信息在cygwin上显示,这次要给前台们能看见的数据,因为数据库里插入的数据少,所以写的语句翻来覆去就那几个词,emmm···当 ...

  5. 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结

    史上最全的CSS hack方式一览 2013年09月28日 15:57:08 阅读数:175473 做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我 ...

  6. Nivo Slider - 世界上最棒的 jQuery 图片轮播插件

    Nivo Slider 号称世界上最棒的图片轮播插件,有独立的 jQuery 插件和 WordPress 插件两个版本.目前下载量已经突破 1,800,000 次!jQuery 独立版本的插件主要有如 ...

  7. Android Studio导入GitHub上的项目常见问题(以图片轮播开源项目为实例)

    前言:github对开发者而言无疑是个宝藏,但想利用它可不是件简单的事,用Android studio导入开源项目会遇到各种问题,今天我就以github上的一个图片轮播项目为例,解决导入过程中的常见问 ...

  8. 安装php rabbitmq扩展,继上一篇安装Rabbitmq

    1 安装 rabbitmq-c,C 与 RabbitMQ 通信需要依赖这个库,这里只贴出正确的步骤,错误类型太多,不一一举例,大部分都是安装问题,缺少组件,安装目录问题 git clone git:/ ...

  9. 继上一篇bootstrap框架(首页)弄的资讯页面

    还是和上一篇首页一样给出每一步的注解: 做的有点简单,但是,以后还是会加深的.毕竟是初学者嘛! <html lang="zh-cn">   <head>   ...

随机推荐

  1. Ajax登陆,使用Spring Security缓存跳转到登陆前的链接

    Spring Security缓存的应用之登陆后跳转到登录前源地址 什么意思? 用户访问网站,打开了一个链接:(origin url)起源链接 请求发送给服务器,服务器判断用户请求了受保护的资源. 由 ...

  2. JavaSE 帮助文档下载

  3. idea | 命名空间改过后重新导入项目方法

    file菜单-->Open-->找到项目中pom文件-->会提示as project 和as file-->选择as project,会自动生成idea所有相关的文件

  4. Codeforces 1139F(树状数组+扫描线)

    题目传送 做法 对于每个人,inc为x,pref为y:对于每道菜,p和s为x,b为y 于是根据题意有\[p[i]<=x<=s[i]\]\[p[i]+b[i]<=x+y\]\[p[i] ...

  5. 栈 && 教授的测试

    卡特兰数:https://blog.csdn.net/wu_tongtong/article/details/78161211 https://www.luogu.org/problemnew/sho ...

  6. BIO,NIO,AIO总结

    熟练掌握 BIO,NIO,AIO 的基本概念以及一些常见问题是你准备面试的过程中不可或缺的一部分,另外这些知识点也是你学习 Netty 的基础. BIO,NIO,AIO 总结 1. BIO (Bloc ...

  7. odoo9 部署步详细步骤

    sudo apt-get updatesudo apt-get dist-upgrade 一:安装和配置pg sudo apt-get install  postgresql sudo su - po ...

  8. C#oracle备份和还原

    最近公司的oracle备份工具不好使了,原来是公司的人用VB写的,由于我是主攻C#的,所以想着自己来写一个C#版本的oracle备份和还原工具. 一开始,我按照原来的设计思路来进行编写,想在pluss ...

  9. 在腾讯云centOs系统上安装nginx

    1.安装nginx 下载:wget http://nginx.org/download/nginx-1.8.1.tar.gz 解压:tar -zxvf nginx-1.8.1.tar.gz 安装编译源 ...

  10. 洛谷P2827 蚯蚓(单调队列)

    题意 初始时有$n$个蚯蚓,每个长度为$a[i]$ 有$m$个时间,每个时间点找出长度最大的蚯蚓,把它切成两段,分别为$a[i] * p$和$a[i] - a[i] * p$,除这两段外其他的长度都加 ...