接到项目, 用react和material-ui实现轮播图. 搜索了一些方法参考, 不论语言/框架的使用,大体上分为两种思路

超宽列表实现法

在原生JS或者JQuery中,轮播图的实现一般是这样子的

设置一个ul, 把所有图片横向展开,复制第一张图到最后,设置显示的界面 正好为一张图的大小和宽度,然后不断向后移动. 到最后一张图时无变化的切换到第一张,

橙色框为显示穿体,可以想象成在后面抽动图片条, 抽到后一个的时候瞬间无动画的回到初始状态.

这个可以类比数据结构中的循环数组,首尾相连,从一个元素开始可以遍历完整个队列然后回到初始元素,类似项链的一个圈.

循环队列显示法

而我们观察到,影响整个动画的只有3个元素,当前图像和前后两个图像,因此可以类比数据结构中的循环队列,每次只记录当前元素和当前元素前后的状态,用index指向当前元素即可

那么就有三种状态

center 显示在屏幕中间的图片

left 即将从屏幕中移除的图片,动画效果直到完全出屏幕

right 即将进入屏幕的图片,动画效果从右侧贴紧屏幕到正中

图片被包裹在一个div中 以下为代码

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { withStyles } from '@material-ui/core/styles'; const styles = ({
root: {
position: "relative",
padding: 0,
margin: 0,
overflow: "hidden",
width: "100%",
},
center: {
position: "relative", //显示在中间的要撑开div
width: "100%",
left: 0, //用于transition动画,必须设定left值
transition :"all 1s ease-in-out",
},
right: {
position :"absolute",
left: "100%",
width: "100%",
top: 0,
},
left: {
position :"absolute",
left: "-100%",
width: "100%",
top: 0,
transition :"all 1s ease-in-out",
}
}); class Slideshow extends Component { state = {
index: 0,
images: [],
}; turn = step => {
let index = this.state.index + step;
if (index >= this.state.images.length) {
index = 0;
}
if (index < 0) {
index = this.props.images.length - 1;
}
this.setState({ index: index })
}; go = () => {
this.timer = setInterval(
() => this.turn(1),
this.props.delay * 1000,
)
}; clear = () => {
clearInterval(this.timer)
}; componentDidMount() {
const images = [
require('../image/1.jpg'),
require('../image/2.jpg'),
require('../image/3.png'),
];
this.setState({
images: images,
});
this.go()
}; componentWillUnmount() {
this.clear();
} render() {
const { classes } = this.props; return (
<div
className={classes.root}
onMouseOver={this.clear} //鼠标悬停时停止计时
onMouseLeave={this.go}
>
{ this.state.images.map((item, index) => (
<img
src={ item }
alt=""
key={index}
className={ classNames(
{ [classes.center]: index === this.state.index },
{
[classes.right]:
index === this.state.index + 1 || (index === 0 && this.state.index === this.state.images.length - 1)
},
{
[classes.left]:
index === this.state.index - 1 || (index === this.state.images.length - 1 && this.state.index === 0)
},
) }/>
))
}
</div>
) }
} Slideshow.propTypes = {
classes: PropTypes.object.isRequired,
}; export default withStyles(styles)(Slideshow);

有了基本框架之后,向左向右翻动的按钮和显示页数的按钮也可以直接做出来了. 只需要改变当前页面的index即可生效

在做按钮的时候发现一个问题,这个方法在只有3张图片的时候 会有奇怪的情况出现.原因是因为 只有3张图片 每一张都是本体 前驱 后继 每次转换的时候都会进行一次transform

解决办法 用4张图片 或者设置left和right 的opacity:0

React 轮播图实现的更多相关文章

  1. reactjs-swiper react轮播图组件基于swiper

    react轮播图组件基于swiper demo地址:http://reactjs-ui.github.io/reactjs-swiper/simple.html 1. 下载安装 npm install ...

  2. react轮播图----react-slick

    1.安装 npm install react-slick; //安装样式 npm install slick carousel; 再在App.css中引入 @import "~slick-c ...

  3. React视角下的轮播图

    天猫购物网站最显眼的就是轮播图了.我在学习一样新js库,一个新框架或新的编程思想的时候,总是感叹"入门必做选项卡,进阶须撸轮播图."作为一个React组件,它是状态操控行为的典型, ...

  4. React中使用CSSTransitionGroup插件实现轮播图

    动画效果,是一个页面上必不可少的功能,学习一个新的东西,当然就要学习,如何用新的东西,用它的方法去实现以前的东西啦.今天呢,我就在这里介绍一个试用react-addons-css-transition ...

  5. React Native学习(六)—— 轮播图

    本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...

  6. React Native 如何做轮播图 react-native-swiper

    //:仿饿了么github:https://github.com/stoneWeb/elm-react-native 欢迎各位同学加入: React-Native群:397885169 大前端群:54 ...

  7. React Native 之轮播图swiper组件

    注释:swiper组件是第三方组件 所以在使用之前应该先在命令行安装,然后将第三方的模块引入(第三方模块地址:https://github.com/leecade/react-native-swipe ...

  8. React Native布局实践:开发京东client首页(三)——轮播图的实现

    上篇文章中,我们一起构建了京东client的TabBar.在本文中.将继续向大家介绍京东client首页轮播图及其下发功能button的开发方法,如今就让我们開始吧! 1.相关控件调研 眼下在Gith ...

  9. ReactNative新手学习之路04 组件化开发轮播图swiper支持安卓和IOS

    react native 新手之路04 组件化开发轮播图swiper支持安卓和IOS npm install react-native-carousel --save git 地址Properties ...

随机推荐

  1. 华硕飞行堡垒fx50 安装ubuntu18.04

    决定把我的渣机脱坑 一.制作启动盘 官方下载ubuntu18.04LTS iso文件 [ubuntu官方链接](https://www.ubuntu.com/download/desktop Ultr ...

  2. VMware Workstation 14安装VMware Tools

    1 单击虚拟机,选择安装VMware Tools 2 此时会在桌面出现VWware Tools 3 双击进入 4 把*.tar.gz压缩文件cp到/home下 5 sudo tar -zvxf  *. ...

  3. 石家庄地铁系统开发(java web版)(二)

    两种方法: 一,自己写数据库,自己写算法实现 二,调用已有软件的API(百度,高德)

  4. iframe 高度自适应

    <iframe id="InputDetail" src style="width:100%"></iframe> <script ...

  5. js获取元素宽高、位置相关知识汇总

    常见clientWidth.clientHeight.offsetWidth.offsetLeft,clientX.scrollTop等词语,比较混乱,现在总结下他们的区别. 1. clientWid ...

  6. Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock32 error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"

     今天安装完带图形界面的CentOS 7后,在Terminal中运行yum安装命令时报了以下错误: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...

  7. Linux odoo开发环境配置

    Linux odoo开发环境配置 安装postgresql9.6 第1步:添加RPM源(通过官网获取下载地址) yum install https://download.postgresql.org/ ...

  8. Lua中ipairs和pairs的区别详解

    迭代器for遍历table时,ipairs和pairs的区别: 区别一:ipairs遇到nil会停止,pairs会输出nil值然后继续下去 区别二: , b = , x = , y = , " ...

  9. 一本通网站 1378:最短路径(shopth)

    [题目描述] 给出一个有向图G=(V, E),和一个源点v0∈V,请写一个程序输出v0和图G中其它顶点的最短路径.只要所有的有向环权值和都是正的,我们就允许图的边有负值.顶点的标号从1到n(n为图G的 ...

  10. 纠错式教学法对比鼓励式教学法 -----Lily、贝乐、英孚,乐加乐、剑桥国际、优学汇、北外青少

    一.关于两种英语教学法的争议 在英语教学方面,主要有纠错式教学法(目前主要对应国内听说读写四位一体的教学法)和鼓励式教学法(目前对应国内听说为主的教学法),这两种教学方法其实是各有千秋,各有利弊的. ...