1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3.  
  4. import {getSwipeWay} from '../utils/swipe';
  5.  
  6. //imgs : 图片src数组
  7. //playTime : 轮播下一张图片的时间间隔
  8. //notAuto : 是否开启自动轮播 默认开启 false
  9.  
  10. const PropsChecker = {
  11. imgs : PropTypes.array.isRequired,
  12. playTime : PropTypes.number,
  13. notAuto : PropTypes.bool,
  14. };
  15.  
  16. class Carousel extends React.Component {
  17. static defaultProps = {
  18. playTime : 5000,
  19. notAuto : false,
  20. }
  21.  
  22. constructor(args){
  23. super(args);
  24. this.state = {
  25. };
  26. //缓存各个currIndex的ul之后的标签
  27. this.storeElements = {};
  28. //判断滑动手势
  29. this.swipeWay = getSwipeWay(50);//闸值50
  30. //图片显示的限制
  31. this.limit = 3;
  32. //当前展示的图片
  33. this.currIndex = 0;
  34. //展示的数组
  35. this.showImgs = [];
  36. //手势滑动坐标
  37. this.position = {
  38. x1:0,
  39. x2:0,
  40. y1:0,
  41. y2:0,
  42. };
  43. //<ul>
  44. this.Ul = null;
  45. //禁止在transiton的期间操作
  46. this.isTransition = false;
  47. //定时器
  48. this.timer = 0;
  49. }
  50.  
  51. componentDidMount(){
  52. this.autoPlay();
  53. }
  54.  
  55. componentWillUnmount(){
  56. clearTimeout(this.timer);
  57. }
  58.  
  59. getHead(arr){
  60. if(Array.isArray(arr)){
  61. return arr[0];
  62. }
  63. console.error('非数组');
  64. }
  65.  
  66. getLast(arr){
  67. if(Array.isArray(arr)){
  68. const len = arr.length;
  69. return arr[len-1];
  70. }
  71. console.error('非数组');
  72. }
  73.  
  74. calcIndex(){
  75. const {imgs} = this.props;
  76. const len = imgs.length;
  77. const limit = this.limit;
  78. const currIndex = this.currIndex;
  79.  
  80. if(currIndex == 0){
  81. this.showImgs = imgs.slice(0,limit - 1);
  82. this.showImgs.unshift(this.getLast(imgs));
  83. return;
  84. }
  85.  
  86. if(currIndex == len - 1){
  87. this.showImgs = imgs.slice(len -2 ,len);
  88. this.showImgs.push(this.getHead(imgs));
  89. return;
  90. }
  91.  
  92. this.showImgs = imgs.slice(currIndex -1 , currIndex + limit -1);
  93.  
  94. }
  95.  
  96. changeCurrIndex(flag){
  97. const {imgs} = this.props;
  98. const last = imgs.length -1;
  99. const currIndex = this.currIndex;
  100. if(flag === '-'){
  101. this.currIndex = currIndex == 0 ? last : currIndex -1 ;
  102. return;
  103. }
  104.  
  105. if(flag === '+'){
  106. this.currIndex = currIndex == last ? 0 : currIndex + 1 ;
  107. return;
  108. }
  109. }
  110.  
  111. ulTranslate(value){
  112. const Ul = this.Ul;
  113. if(Ul){
  114.  
  115. if(Ul.style.webkitTranslate ){
  116. Ul.style.webkitTranslate = value;
  117. }else{
  118. Ul.style.translate = value;
  119. }
  120.  
  121. }
  122.  
  123. }
  124.  
  125. createUl(){
  126. const currIndex = this.currIndex;
  127. const storeElements = this.storeElements;
  128.  
  129. //缓存这些标签,避免多次创建,很有必要
  130. if(!storeElements[currIndex]){
  131. //要保证<ul>key不同 也就是每次轮播后都要是新的标签,有损性能
  132. const Ul = (<ul onTouchEnd={this.touchEnd}
  133. onTouchMove={this.touchMove}
  134. onTouchStart={this.touchStart}
  135. onTransitionEnd={this.transitionEnd} ref={(ele)=>this.Ul=ele} key={currIndex}>
  136.  
  137. {this.createLis()}
  138. </ul>);
  139.  
  140. storeElements[currIndex] = Ul;
  141. }
  142.  
  143. return storeElements[currIndex];
  144. }
  145.  
  146. createLis(){
  147. this.calcIndex();
  148. const imgs = this.showImgs;
  149.  
  150. return imgs.map((src,i)=>{
  151. const liStyle = {
  152. // translate:(-i)+'00%',
  153. translate:( (i+'00') - 100 ) + '%',
  154. WebkitTranslate:( (i+'00') - 100 ) + '%',
  155. };
  156.  
  157. return <li className='item' key={i} style={liStyle} ><img src={src} /></li>;
  158. });
  159.  
  160. }
  161.  
  162. touchStart = (e) => {
  163. if(this.isTransition){
  164. return;
  165. }
  166.  
  167. clearTimeout(this.timer);
  168.  
  169. const {clientX,clientY} = e.touches[0];
  170. this.position.x1 = clientX;
  171. this.position.y1 = clientY;
  172. }
  173.  
  174. touchMove = (e) => {
  175.  
  176. }
  177.  
  178. touchEnd = (e) => {
  179. if(this.isTransition){
  180. return;
  181. }
  182.  
  183. const {clientX,clientY} = e.changedTouches[0];
  184. this.position.x2 = clientX;
  185. this.position.y2 = clientY;
  186.  
  187. const {x1,x2,y1,y2} = this.position;
  188.  
  189. const direction = this.swipeWay(x1,x2,y1,y2);
  190.  
  191. if( direction === 'Left'){
  192. this.changeCurrIndex('+');
  193. this.isTransition = true;
  194. this.ulTranslate('-100%');
  195. }
  196.  
  197. if(direction === 'Right'){
  198. this.changeCurrIndex('-');
  199. this.isTransition = true;
  200. this.ulTranslate('100%');
  201. }
  202. }
  203.  
  204. next(){
  205. this.changeCurrIndex('+');
  206. this.isTransition = true;
  207. this.ulTranslate('-100%');
  208. }
  209.  
  210. autoPlay(){
  211. const {playTime,notAuto} = this.props;
  212. if(!notAuto){
  213. this.timer = setTimeout(()=>{
  214. this.next();
  215. },playTime);
  216. }
  217. }
  218.  
  219. transitionEnd = (e) => {
  220. this.forceUpdate(()=>{
  221. this.isTransition = false;
  222. this.autoPlay();
  223. });
  224. }
  225.  
  226. render(){
  227.  
  228. return (<div className='mm-carousel' >
  229. {this.createUl()}
  230. </div>);
  231. }
  232. }
  233.  
  234. export default Carousel;
  235.  
  236. 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. [Xcode 实际操作]三、视图控制器-(3)使用UINavigationController视图控制器

    目录:[Swift]Xcode实际操作 本文将演示导航视图控制器的使用. 选择项目导航区的资源文件夹.需要导入两张图片,作为选项卡控制器的图标. [+]->[Import]->选择图片-& ...

  2. 当项目只有src文件和web文件时eclipse如何导入javaweb工程

    原理是:利用工具生成class文件,并且在过程中检查出错误,生成对应的编译后文件,这样才能在tomcat等服务器上跑,服务器上只能跑编译后的文件. 1. 2. 3. . 4. 5. 6. 7.

  3. css实现发光的input输入框

    效果图截图: 案例代码示下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  4. HDU4405(期望dp)

    标准期望套路,很水.读题看好是到n就可以停止了. ; int n, m; db dp[maxn]; map<int, int> mp; int main() { while (~scanf ...

  5. 类加载机制 + Classloader.loadClass(String name)和Class.forName(String name)

    Classloader.loadClass(String name)和Class.forName(String name)的区别 Java的类在jvm中的加载大致分为加载,链接或者叫link(里面包含 ...

  6. 字符串与C51的格式化输出

    一字符数组和字符指针: 字符指针可以用字符串对其直接初始化和随时赋值:而字符数组可以用字符串进行初始化,但不能用字符串对其进行随时赋值(但此时可以定义一个字符串指针指向字符数组,然后用字符串对指针随时 ...

  7. Linux--NiaoGe-Service-07网络安全与主机基本防护

    Linux系统内自带的防火墙有两层: 第一层:数据包过滤防火墙:IP Filtering和Net Filter 要进入Linux本机的数据包都会先通过Linux预先内置的防火墙(Net Filter) ...

  8. ecshop文章分类页面调用文章的内容

    有的时候需要用到,所以总结了一下. 打开includes/lib_article.php文件 红色部分为添加的部分 function get_cat_articles($cat_id, $page = ...

  9. 灰度共生矩阵GLCM分析

    纹理分析是对图像灰度(浓淡)空间分布模式的提取和分析.纹理分析在遥感图像.X射线照片.细胞图像判读和处理方面有广泛的应用.关于纹理,还没有一个统一的数学模型.它起源于表征纺织品表面性质的纹理概念,可以 ...

  10. 阻塞 io 非阻塞 io 学习笔记

    阻塞 io 非阻塞 io 学习笔记