forked from godaddy/react-img-carousel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
146 lines (135 loc) · 5.95 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React, { Component } from 'react';
import { render } from 'react-dom';
import Carousel from './src/index.js';
require('./src/carousel.less');
class CustomDots extends React.Component {
render () {
const { numSlides, selectedIndex, goToSlide } = this.props;
const dots = [];
for (let index = 0; index < numSlides; index++) {
const buttonStyle = {
border: 'none',
cursor: 'pointer',
background: 'transparent'
};
if (index === selectedIndex) {
buttonStyle.color = 'red';
}
dots.push(
<li key={ `dot-${index}` } style={ { display: 'inline-block' } }>
<button style={ buttonStyle } onClick={ goToSlide.bind(null, index) }>•</button>
</li>
);
}
return (
<div style={ { position: 'absolute', top: '10px', right: '10px', background: 'rgba(114, 114, 114, 0.6)', zIndex: '1' } }>
<h2>{ this.props.title }</h2>
<ul style={ { listStyle: 'none', padding: '0' } }>
{ dots }
</ul>
</div>
);
}
}
const IMAGES = [
'http://lorempixel.com/400/300',
'http://lorempixel.com/275/300',
'http://lorempixel.com/400/300',
'http://lorempixel.com/350/300',
'http://lorempixel.com/250/300',
'http://lorempixel.com/375/300',
'http://lorempixel.com/425/300',
'http://lorempixel.com/325/300'
];
let imgIndex = 1;
class TestPage extends Component {
constructor () {
super(...arguments);
this.state = {
images: [IMAGES[0]]
};
this.addImage = this.addImage.bind(this);
}
render () {
const { images } = this.state;
const imgElements = IMAGES.map((image, index) => <img src={ image } key={ index } />);
return (
<div className='container' style={ { textAlign: 'center' } }>
<h2 style={ { marginTop: 80 } }>Infinite with cell padding</h2>
<Carousel width='450px' cellPadding={ 5 }>
{ imgElements }
</Carousel>
<h2 style={ { marginTop: 80 } }>Non-Infinite with cell padding</h2>
<Carousel infinite={ false } width='450px' cellPadding={ 5 }>
{ imgElements }
</Carousel>
<h2 style={ { marginTop: 80 } }>Fade transition</h2>
<Carousel width='450px' slideHeight='300px' transitionDuration={ 1000 } draggable={ false } transition='fade'>
{ imgElements }
</Carousel>
<h2 style={ { marginTop: 80 } }>Infinite with only 2 slides</h2>
<Carousel width='450px' arrows={ false } slideHeight='300px'>
<img src='http://lorempixel.com/325/300'/>
<img src='http://lorempixel.com/350/300'/>
</Carousel>
<h2 style={ { marginTop: 80 } }>Infinite with only 1 slide</h2>
<Carousel width='450px' infinite={ true } arrows={ false } dots={ false }>
<img src='http://lorempixel.com/325/300'/>
</Carousel>
<h2 style={ { marginTop: 80 } }>Autoplay with background images</h2>
<Carousel width='100%' slideWidth='100%' slideHeight='70vh' arrows={ false } autoplay={ true }>
<div style={ { backgroundImage: 'url(http://lorempixel.com/600/300)', backgroundSize: 'cover', height: '100%', width: '100%' } }/>
<div style={ { backgroundImage: 'url(http://lorempixel.com/650/300)', backgroundSize: 'cover', height: '100%', width: '100%' } }/>
<div style={ { backgroundImage: 'url(http://lorempixel.com/675/300)', backgroundSize: 'cover', height: '100%', width: '100%' } }/>
<div style={ { backgroundImage: 'url(http://lorempixel.com/700/300)', backgroundSize: 'cover', height: '100%', width: '100%' } }/>
</Carousel>
<h2 style={ { marginTop: 80 } }>Background images with fade</h2>
<Carousel width='100%' slideWidth='100%' slideHeight='70vh' transition='fade' transitionDuration={ 1000 } autoplay={ true } arrows={ true }>
<div style={ { backgroundImage: 'url(http://lorempixel.com/600/300)', backgroundSize: 'cover', height: '100%', width: '100%' } }/>
<div style={ { backgroundImage: 'url(http://lorempixel.com/650/300)', backgroundSize: 'cover', height: '100%', width: '100%' } }/>
<div style={ { backgroundImage: 'url(http://lorempixel.com/675/300)', backgroundSize: 'cover', height: '100%', width: '100%' } }/>
<div style={ { backgroundImage: 'url(http://lorempixel.com/700/300)', backgroundSize: 'cover', height: '100%', width: '100%' } }/>
<div style={ { backgroundImage: 'url(http://lorempixel.com/750/300)', backgroundSize: 'cover', height: '100%', width: '100%' } }/>
<div style={ { backgroundImage: 'url(http://lorempixel.com/725/300)', backgroundSize: 'cover', height: '100%', width: '100%' } }/>
<div style={ { backgroundImage: 'url(http://lorempixel.com/625/300)', backgroundSize: 'cover', height: '100%', width: '100%' } }/>
</Carousel>
<h2 style={ { marginTop: 80 } }>Custom dots component</h2>
<Carousel
width='450px'
cellPadding={ 5 }
infinite={ false }
dots={ false }
arrows={ false }
autoplay={ true }
controls={ [{ component: CustomDots, props: { title: 'My Slides' }, position: 'top' }] }
transitionDuration={ 0 }
>
{ imgElements }
</Carousel>
<h2 style={ { marginTop: 80 } }>Add images</h2>
<Carousel
width='450px'
cellPadding={ 5 }
infinite={ true }
dots={ false }
arrows={ false }
autoplay={ false }
controls={ [{ component: CustomDots, props: { title: 'My Slides' }, position: 'top' }] }
>
{
images.map((image, index) => <img key={ index } src={ image }/>)
}
</Carousel>
<button onClick={ this.addImage }>Add Image</button>
</div>
);
}
addImage () {
const { images } = this.state;
if (imgIndex < IMAGES.length) {
const newImages = images.concat([IMAGES[imgIndex++]])
this.setState({ images: newImages });
}
}
}
render(<TestPage />, document.body);