React 轮播图插件
来源:互联网 发布:电陶炉 知乎 编辑:程序博客网 时间:2024/06/09 18:28
文件目录:
src-| |-Sliders-| |-components-| | |-Sliders.js | |-SlidersArrows.js | |-SlidersDots.js | |-SlidersItem.js |-css-| | |-Sliders.css | |-img-| | |-图片 | |-index.js
引用插件文件设置:
import React,{Component} from 'react';import ReactDOM from 'react-dom';import Sliders from './components/Sliders';const Img=[ require('./img/1.gif'), require('./img/2.gif'), require('./img/3.jpg'), require('./img/4.jpg'),]ReactDOM.render( <Sliders images={Img} speed={1} delay={2} autoPlay={true} autoParse={true} />,document.querySelector('#root'));
js:
1、 Sliders.js
import React,{Component} from 'react';import '../css/Sliders.css';import SlidersItem from './SlidersItem';import SlidersArrows from './SlidersArrows';import SlidersDots from './SlidersDots';export default class Sliders extends Component{ constructor(){ super(); this.state={index:0} } componentDidMount(){ if(this.props.autoPlay){ this.go(); } } go=()=>{ this.timer=setInterval(()=>{ this.turn(1) },this.props.delay*1000) }; turn=(step)=>{ let index= this.state.index+step; if(index>=this.props.images.length){ index=0; } if(index<0){ index=this.props.images.length-1; } this.setState({index:index}) }; render(){ return ( <div className="wrapper" onMouseOver={()=>clearInterval(this.timer)} onMouseOut={this.go} > <SlidersItem images={this.props.images} speed={this.props.speed} index={this.state.index} /> <SlidersArrows turn={this.turn} /> <SlidersDots images={this.props.images} turn={this.turn} index={this.state.index} /> </div> ) }};
2、 SlidersItem.js
import React,{Component} from 'react';export default class SlidersItem extends Component{ render(){ let style={ width:this.props.images.length*500+'px', left:this.props.index*-500+'px', transitionDuration:this.props.speed+'s' }; return ( <ul className="sliders" style={style}> { this.props.images.map((item,index)=>( <li className="slider" key={index} > <img src={item} alt=""/> </li> )) } </ul> ) }}
3、 SlidersArrows.js
import React,{Component} from 'react';export default class SlidersArrows extends Component{ render(){ return ( <div className="arrows" > <span className="arrow arrow-left" onClick={()=>this.props.turn(-1)} > < </span> <span className="arrow arrow-right" onClick={()=>this.props.turn(1)} > > </span> </div> ) }}
4、 SlidersDots.js
import React,{Component} from 'react';export default class SlidersDots extends Component{ render(){ return ( <div className="dots" > { this.props.images.map((item,index)=>( <span key={index} className={"dot "+(index==this.props.index?'active':'')} onClick={()=>this.props.turn(index-this.props.index)} > </span> )) } </div> ) }}
css:
*{ padding: 0; margin: 0;}ul,li{ list-style: none;}.wrapper{ position: relative; width: 500px; margin: 30px auto; height: 300px; overflow: hidden;}.sliders{ position: absolute; height: 300px;}.slider{ float: left; width: 500px; height: 300px;}.slider img{ width: 100%; height: 100%;}.arrows{ position: absolute; top: 50%; height: 20px; width: 100%;}.arrow{ position: absolute; width: 20px; height: 20px; line-height: 20px; text-align: center; background: #ccc; cursor: pointer;}.arrow-left{ left: 10px;}.arrow-right{ right:10px;}.dots{ position: absolute; bottom: 10px; width: 100%; height: 20px; line-height: 20px; text-align: center;}.dot{ display: inline-block; width: 20px; height: 20px; margin: 0 3px; background-color: #ccc;}.dot.active{ background-color: deepskyblue;}
阅读全文
0 0
- React 轮播图插件
- React插件及动画插件
- React插件及动画
- React Native调试插件
- react常用插件大全
- react.js 翻页插件
- react-native插件
- React Js 生成二维码 插件 qrcode.react
- React 开发必备插件 React Developer Tools
- React插件-双向绑定辅助工具
- React插件-类名操作
- React-插件-性能分析工具
- React插件-双向绑定辅助工具
- React插件-类名操作
- React-插件-性能分析工具
- react native常用插件地址
- react-native 三方插件
- atom中的react 插件汇总
- 【PHP自学笔记】第十三章 面向对象编程
- SpringMVC返回json数据的三种方式
- HTML学习笔记(二)
- 转载-Python UnicodeDecodeError: 'gbk' codec can't decode byte 0xe9
- Extjs之FormPanel布局
- React 轮播图插件
- 下载Android源码流程(完整版)
- 【PHP自学笔记】第十七章 PHP的字符编码
- java数组打印字母的等腰三角形
- requests模拟登陆(session+url_token)
- “戏”说Spark-Spark核心-RDD转换操作算子详解(二)
- 字符串的输入和输出
- hrbust 2291
- 生活小记61