react 引入轮播插件_React.js实现轮播图
react.js的基本思想,是通过改变state或props的值,重新渲染用户界面(不用操作DOM)。截图GIF效果如下(只截取了三页效果):
GIF1.gif
1.文件列表:
1490606951(1).jpg
2.组件功能说明:
1.可控制滚动方向(上下左右)
2.可控制轮播图片的宽高
3.可控制轮播图片的数量
4.可控制轮播图片停留时间
5.可控制轮播的风格
6.轮播的基本功能(dots,左右按钮,鼠标滑入滑出的暂停与播放,无缝)
3.组件使用说明:
用户修改json中的数据即可使用,
imgArray ---------------------------- 数组类型,放置轮播图片的地址(必填)
linkArray ---------------------------- 数组类型,放置点击图片访问的地址(必填)
lunboObject ------------------------ json类型,放置一些轮播图的基本设置(必填)
interval ---------------------------- number类型,设置图片停留时间
direction -------------------------- string类型,设置运动的方向
number --------------------------- number类型,设置图片数量(必填)
boxStyle -------------------------- string类型,设置某些地方的样式
imgWidth ------------------------- number类型,设置图片宽度(必填)
imgHeight ------------------------ number类型,设置图片高度(必填)
4.组件内容:
1.data.json
"imgArray": [
"../src/pages/lunbo/img/1.jpg",
"../src/pages/lunbo/img/2.jpg",
"../src/pages/lunbo/img/3.jpg",
"../src/pages/lunbo/img/4.jpg",
"../src/pages/lunbo/img/5.jpg",
"../src/pages/lunbo/img/6.jpg",
"../src/pages/lunbo/img/7.jpg"
],
"linkArray": [
"http://bj.ganji.com",
"http://bj.ganji.com",
"http://bj.ganji.com",
"http://bj.ganji.com",
"http://bj.ganji.com",
"http://bj.ganji.com",
"http://bj.ganji.com"
],
"lunboObject": {
"interval": 1000,
"direction": "right",
"number": 7,
"boxStyle": "content",
"imgWidth": 550,
"imgHeight": 350
}
}
2.lunbo.jsx
var style = require('./lunbo.less');
var ReactDOM = require('react-dom');
var React = require('react');
var data = require('./data.json');
var LunBoControl = React.createClass({
/*对出入的props进行验证*/
propsTypes : {
defaultActiveIndex:React.PropTypes.number,
interval:React.PropTypes.number,
direction:React.PropTypes.oneOf['right','left','top','bottom'],
number:React.PropTypes.number,
boxStyle:React.PropTypes.string,
imgWidth:React.PropTypes.number.isRequired,
imgHeight:React.PropTypes.number.isRequired
},
/*设置默认的props值*/
getDefaultProps: function(){
return {
direction:'right',
interval: 1000,
boxStyle:'content'
};
},
/*初始化state值*/
getInitialState : function(){
return{
activeIndex:1,
offsetDistance:this.props.direction == 'right' || this.props.direction == 'left' ? -this.props.imgWidth : -this.props.imgHeight,
pause:false,
flag:true
};
},
/*生命周期函数 在首次渲染之前*/
componentWillMount : function(){
this.direction = this.props.direction === 'left' || this.props.direction === 'right'? 'x' : 'y';
},
/*在真实的DOM被渲染出来后*/
componentDidMount : function(){
this.autoPlay();
},
/*组件被移除之前*/
componentWillUnmount : function(){
clearTimeout(this.timeOuter);
clearInterval(this.timer);
},
autoPlay : function(){
switch(this.props.direction){
case 'right' :
this.timerOuter=setTimeout(this.playRight,this.props.interval);
this.direction='x';
break;
case 'left' :
this.timerOuter=setTimeout(this.playLeft,this.props.interval);
this.direction='x';
break;
case 'top' :
this.timerOuter=setTimeout(this.playLeft,this.props.interval);
this.direction='y';
break;
case 'bottom':
this.timerOuter=setTimeout(this.playRight,this.props.interval);
this.direction='y';
break;
};
},
/*对不同方向做的相应模板上样式的处理*/
directionHandle : function(){
if(this.direction === 'y'){
return {top : this.state.offsetDistance+'px',width : this.props.imgWidth,height : this.props.imgHeight*(this.props.number+2)};
}else {
return {left : this.state.offsetDistance+'px',width : this.props.imgWidth*(this.props.number+2),height : this.props.imgHeight};
}
},
/*鼠标滑入,滑出*/
mouseHandle : function(e){
if(e.type === 'mouseover'){
this.setState({pause : true});
}else if(e.type === 'mouseleave'){
this.setState({pause : false});
this.autoPlay();
}
},
/*圆点显示效果*/
checkDots : function(index){
var activeIndex;
if(this.state.activeIndex === this.props.number+1){
activeIndex = 1;
}else if(this.state.activeIndex === 0){
activeIndex = this.props.number;
}else {
activeIndex = this.state.activeIndex;
}
return index+1 === activeIndex? 'dots active' : 'dots';
},
/*鼠标滑入圆点*/
dotsHover : function(index){
clearInterval(this.timer);
this.setState({activeIndex:index+1});
this.position();
},
/*向右或向下*/
playRight: function(indexIn){
if(this.state.flag){
var index=indexIn?indexIn:this.state.activeIndex+1;
this.setState({activeIndex:index});
this.position();
}
},
/*向左或向上*/
playLeft: function(indexIn){
if(this.state.flag){
var index=indexIn?indexIn:this.state.activeIndex-1;
this.setState({activeIndex:index});
this.position();
}
},
/*运动效果*/
position: function(){
this.setState({flag:false});
this.timer = setInterval(function(){
if(this.direction === 'x'){
var boxDistance = this.props.imgWidth;
}else {
var boxDistance = this.props.imgHeight;
}
var offsetDistance = this.state.offsetDistance;
if(Math.abs(offsetDistance-(-boxDistance*this.state.activeIndex)) <= 0.09){
offsetDistance = -boxDistance*this.state.activeIndex;
clearInterval(this.timer);
this.setState({flag:true});
if(this.state.activeIndex > this.props.number){
offsetDistance = -boxDistance;
this.setState({activeIndex : 1});
}else if(this.state.activeIndex === 0){
offsetDistance = -boxDistance*this.props.number;
this.setState({activeIndex : this.props.number});
}
this.setState({offsetDistance:offsetDistance});
if(!this.state.pause){
this.autoPlay();
}
}else{
offsetDistance = offsetDistance-(boxDistance*this.state.activeIndex-Math.abs(offsetDistance))/30;
this.setState({offsetDistance:offsetDistance});
}
}.bind(this),10);
},
/*点击向左按钮*/
left: function(){
var oldIndex=this.state.activeIndex;
this.playLeft(oldIndex-1);
},
/*点击向右按钮*/
right: function(){
var oldIndex=this.state.activeIndex;
this.playRight(oldIndex+1);
},
render : function(){
var _this = this;
return (
{
React.Children.map(this.props.children,function(elem,index){
return ();
})
}
{this.props.children[this.props.number-1]}
{this.props.children}
{this.props.children[0]}
);
}
});
var LunBoComponent = React.createClass({
propsTypes : {
lunboObject : React.PropTypes.object.isRequired,
imgArray : React.PropTypes.array.isRequired,
linkArray : React.PropTypes.array
},
render : function(){
return (
{
this.props.imgArray.map(function(item,index){
return
;
}.bind(this))
}
);
}
});
module.exports = LunBoComponent;
/*引用按以下方式*/
var LunBoComponent = require('./lunbo.jsx');
ReactDOM.render(, document.getElementById('wrapper'));
3.lunbo.less
li {
float: left;
margin: 0;
padding: 0;
font-size: 12px;
}
img {
vertical-align: top;
}
#wrapper > div {
overflow: hidden;
position: relative;
margin: 0 auto;
margin-top: 50px;
}
ul {
margin: 0;
padding: 0;
list-style: none;
position: relative;
}
.lunbo-item {
float: left;
}
.leftIcon {
position: absolute;
display: inline-block;
width: 25px;
height: 25px;
transform: rotate(-45deg);
top: 162px;
left: 10px;
border-top: 3px solid #00ffcc;
border-left: 3px solid #00ffcc;
z-index: 999;
text-indent: -100%;
cursor: pointer;
}
.rightIcon {
position: absolute;
display: inline-block;
width: 25px;
height: 25px;
transform: rotate(45deg);
top: 162px;
right: 10px;
border-right: 3px solid #00ffcc;
border-top: 3px solid #00ffcc;
z-index: 999;
text-indent: -100%;
cursor: pointer;
}
.dots {
display: inline-block;
width: 16px;
height: 16px;
background: #eee;
border-radius: 8px;
margin-right: 8px;
float: left;
cursor: pointer;
}
.dots.active {
background: #000;
}
.dots-wrap {
overflow: hidden;
position: absolute;
z-index: 99;
bottom: 10px;
right: 20px;
}
react 引入轮播插件_React.js实现轮播图相关推荐
- 点击左右有缝轮播html,超帅轮播插件tabstools.js教程之实现数字+箭头+多栏轮播
摘要: 前面我们讲了tabstools.js插件可实现多种轮播,如:选项卡轮播.数字轮播.缩略图轮播.卡盘轮播.等等,今天我们再来了解下多栏轮播效果...... 万万没想到!写这篇文章竞是在两个月之后 ...
- 轮播插件swiper.js?
首先引入swiper.css和swiper.js,然后引入html,并为每一个div添加一个新的类并设置每个div的背景图片进行轮播,最后初始化轮播js. 转载于:https://www.cnblog ...
- html新闻轮播插件,jQuery新闻类轮播图插件sliderBox
sliderBox.js是一款jQuery新闻类轮播图插件.该jQuery新闻类轮播图插件可以创建兼容ie8.带缩略图导航.以及多种过渡效果的轮播图. 使用方法 在页面中引入jQuery.slider ...
- 轮询机制php,JS事件轮询机制讲解
JS是单线程语言,深入理解JS里的Event Loop,本文主要和大家分享JS事件轮询机制,希望能帮助到大家. JS的执行机制(一): 1.首先判断JS是同步还是异步,同步就进入主进程,异步就进入ev ...
- html灵活响应 图片设置,jQuery轻量级响应式图片轮播插件ResponsiveSlides.js(仅1kb)
ResponsiveSlides.js ResponsiveSlides.js是一个展示同一容器内图片的轻量级响应式jQuery幻灯片插件(tiny responsive slideshow jQue ...
- Bootstrap JavaScript插件:轮播插件 (carousel.js)
作者:WangMin 格言:努力做好自己喜欢的每一件事 CSDN原创文章 博客地址
- php轮播插件,移动端h5轮播插件swipe实例详解
swipe.js是一个轻量级js触摸滑动类库 – Swipe JS.这是一个非常小的一个javascript类库,但他的功能却不简单,它可以用来展示web页面上的任何内容,支持精确的触摸移动操作,而且 ...
- vue中引入全年日历插件calendar.js
针对依赖amazeui的全年日历插件在vue项目中的问题 cnpm i jquery cnpm i amazeui 在当前vue中引入jquery .amazeui及相关css或这全局引入(看其他是否 ...
- 自动图片轮播php源码,js图片自动轮播代码分享(js图片轮播)
1.利用图片width显示位置来播放图片,技术:.offsetWidth .aBtn[i].index = i .setInterval() .oUl[0].style.left = .onclic ...
最新文章
- R构建幂回归模型(Power Regression)
- Pygame实战:风靡全球的经典泡泡龙小游戏来袭,你会喜欢嘛?(附源码)
- 美元汇率pascal程序
- boost::math::policies用法的测试程序
- mysql恢复语句报错_php对于mysql恢复数据的时候,只能恢复一条!然后就会报错!但把sql语句直接贴到数据库里面是可以执行的!...
- html 英文文字纵向排列,CSS几种简单方法实现文字竖向排版
- 基础选择器之通配符选择器(CSS、HTML)
- AutoHotkey 命令列表
- maven配置访问nexus私服,从nexus私服下载依赖
- AutoCAD Eagle的常规操作和PCB制板及拼板说明
- php opendir(),PHP opendir()用法及代码示例
- 免费遥感图像数据共享网汇总
- 共模和差模信号及其噪音抑制
- input限制上传数量,规定图片上传数量
- Unity3D 学习笔记(六) 手柄配置
- php格林威治时间,PHP默认时间是格林威治时间。
- 龙的战争 Dragon ‘s War
- RSD 教程 —— §2.2 第1次运行的配置
- 前端VUE3+Vite -- 框架搭建
- VSCode更新到1.42.1版本有问题(January 2020 (version 1.42))