继上一篇随笔,优化3张以上图片轮播React组件
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组件的更多相关文章
- VS2010/VS2013项目创建及通过ADO.NET连接mysql/sql server步骤(VS2013连接成功步骤见上一篇随笔)
本随笔主要是对初学者通过ADO.NET连接数据库的步骤(刚开始我也诸多不顺,所以总结下,让初学者熟悉步骤) 1.打开VS新建一个项目(这里的VS版本不限,建项目都是一样的步骤) VS2010版本如图: ...
- iOS开发UI篇—UIScrollView控件实现图片轮播
iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 二.实现代码 storyboard中布局 代码: #import "YYV ...
- 【转】 iOS开发UI篇—UIScrollView控件实现图片轮播
原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 ...
- 使用Ajax+jQuery来实现前端收到的数据在console上显示+简单的主页设计与bootstrap插件实现图片轮播
1.实现前端输入的数据在console上显示 上一篇是解决了在前端的输入信息在cygwin上显示,这次要给前台们能看见的数据,因为数据库里插入的数据少,所以写的语句翻来覆去就那几个词,emmm···当 ...
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
史上最全的CSS hack方式一览 2013年09月28日 15:57:08 阅读数:175473 做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我 ...
- Nivo Slider - 世界上最棒的 jQuery 图片轮播插件
Nivo Slider 号称世界上最棒的图片轮播插件,有独立的 jQuery 插件和 WordPress 插件两个版本.目前下载量已经突破 1,800,000 次!jQuery 独立版本的插件主要有如 ...
- Android Studio导入GitHub上的项目常见问题(以图片轮播开源项目为实例)
前言:github对开发者而言无疑是个宝藏,但想利用它可不是件简单的事,用Android studio导入开源项目会遇到各种问题,今天我就以github上的一个图片轮播项目为例,解决导入过程中的常见问 ...
- 安装php rabbitmq扩展,继上一篇安装Rabbitmq
1 安装 rabbitmq-c,C 与 RabbitMQ 通信需要依赖这个库,这里只贴出正确的步骤,错误类型太多,不一一举例,大部分都是安装问题,缺少组件,安装目录问题 git clone git:/ ...
- 继上一篇bootstrap框架(首页)弄的资讯页面
还是和上一篇首页一样给出每一步的注解: 做的有点简单,但是,以后还是会加深的.毕竟是初学者嘛! <html lang="zh-cn"> <head> ...
随机推荐
- Bloomberg 的一些功能
FFLO: 查看ETF流动,注意在View点击Contries后选择Asia,查看亚洲流动. 随后对感兴趣的国家点击查看具体股票的流动 关闭Launchpad View之后再次打开: BLP 修改La ...
- MongoDb 创建用户以及其他版本造成的一些问题
问题:require auth data to have schema version 3 but found 1 这是可以查看如下链接: http://stackoverflow.com/quest ...
- __str__,__repr__
目录 __str__ __repr__ __str__ 打印时触发 class Foo: pass obj = Foo() print(obj) <__main__.Foo object at ...
- DRF教程6-分页
rest框架提供自定义分页样式,让你修改再每个页面上显示多少条数据, pagination API 可以: 分页链接作为响应内容的一部分 分页链接包含在响应头里,比如Content-Range or ...
- 应用性能监控-web系统
1 系统规划 参考https://mp.weixin.qq.com/s/UlnHOaN0xaA0jfg5CEmLRA 1.1 数据采集的原则: 数据采集,说起来比较简单,只要把数据报上来就行,具体怎么 ...
- 抛出异常-throws和throw
package com.mpp.test; import java.util.Scanner; public class TryDemoFour { public static void main(S ...
- 1-26HashSet简介
Set的特点 Set里面存储的元素不能重复,没有索引,存取顺序不一致. package com.monkey1024.set; import java.util.HashSet; /** * Set的 ...
- Ubuntu-apt安装Jenkins
系统环境: Ubuntu 16.0.4 2CPU,8G 1.默认Ubuntu软件包里没有Jenkins 2.系统里添加存储密钥 wget -q -O - https://pkg.jenkins.io/ ...
- [已读]了不起的Node.js
2015/1/22 昨天下班前看完了这本,也不算看完,redis与mysql部分没有去翻,觉得暂时用不上. 觉得第一部分的内容还不错. 第二部分主要讲fs,tcp和http这三个模块. 第三个部分是例 ...
- 在spring的过滤器中注入实体类(@autowire会失效可使用这个方法)
转载:难得可贵的好文章 https://blog.csdn.net/chl191623691/article/details/78657638 首先,本文 绝对是好文!不止本文,作者的文章都是很经 ...