Slider 图片轮播
如果项目中使用的是 0.x 版本的基础组件(@icedesign/base, @ali/ice, @alife/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
npm install @alifd/next@latest -S
Guide
轮播组件,就是以幻灯片的方式,在页面中横向展示诸多内容的组件。轮播内容相互独立,前后在内容以及数据上都不存在逻辑关系。
何时使用
单图轮播:该样式通常用于承载运营banner,是一个位置相对固定的模块。
多图轮播:单元信息浏览
使用注意点
当轮播组件中只有一张图片的时候,轮播组件会隐藏导航锚点、禁止自动循环(即使上层设置了)、禁止拖拽播放(即使上层设置了)。
如果您要将 Slider 放到 Dialog 中,此时你需要关闭 Dialog 的动画,避免 Slider 在计算内部元素宽度时造成出错。
如果出现图片高度 1px 的问题,可以尝试在
img
标签的外部包裹一层div
标签。
API
Slider
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
adaptiveHeight | 是否使用自适应高度 | Boolean | false |
animation | 动效类型,默认是'slide' | String/Boolean | 'slide' |
arrows | 是否显示箭头 | Boolean | true |
arrowSize | 导航箭头大小 可选值: 'medium', 'large'可选值:'medium', 'large' | Enum | 'medium' |
arrowPosition | 导航箭头位置 可选值: 'inner', 'outer'可选值:'inner', 'outer' | Enum | 'inner' |
arrowDirection | 导航箭头的方向 可选值: 'hoz', 'ver'可选值:'hoz', 'ver' | Enum | 'hoz' |
autoplay | 是否自动播放 | Boolean | false |
autoplaySpeed | 自动播放的速度 | Number | 3000 |
nextArrow | 向后箭头 | ReactElement | null |
prevArrow | 向前箭头 | ReactElement | null |
centerMode | 是否启用居中模式 | Boolean | false |
dots | 是否显示导航锚点 | Boolean | true |
dotsDirection | 导航锚点位置可选值:'hoz', 'ver' | Enum | 'hoz' |
dotRender | 自定义导航锚点签名:Function() => void | Function | - |
draggable | 是否可拖拽 | Boolean | true |
infinite | 是否使用无穷循环模式 | Boolean | true |
defaultActiveIndex | 初始被激活的轮播图 | Number | 0 |
lazyLoad | 是否启用懒加载 | Boolean | false |
slideDirection | 轮播方向可选值:'hoz', 'ver' | Enum | 'hoz' |
slidesToShow | 同时展示的图片数量 | Number | 1 |
slidesToScroll | 同时滑动的图片数量 | Number | 1 |
speed | 轮播速度 | Number | 500 |
activeIndex | 跳转到指定的轮播图(受控) | Number | - |
triggerType | 锚点导航触发方式可选值:'click', 'hover' | Enum | 'click' |
onChange | 轮播切换的回调函数签名:Function(index: Number) => void参数:index: {Number} 幻灯片的索引 | Function | () => {} |
centerPadding | Side padding when in center mode (px or %); 展示部分为center,pading会产生前后预览 | String | '50px' |
cssEase | CSS3 Animation Easing,默认‘ease’ | String | 'ease' |
focusOnSelect | 多图轮播时,点击选中后自动居中 | Boolean | false |
说明
next-slider is forked from react-slick
代码示例
轮播组件共有两种类型:单图轮播和多图同时轮播。在默认模式下(不指定任何属性值),轮播组件为单图轮播模式。
注意: 如果出现图片 1px 高度的问题,建议将图片的外部包括一层 div
来避免这个问题。
查看源码在线预览
import { Slider } from '@alifd/next';
const slides = [
{ url: 'https://img.alicdn.com/tps/TB1bewbNVXXXXc5XXXXXXXXXXXX-1000-300.png', text: 'Tape Player Skin Design Competition' },
{ url: 'https://img.alicdn.com/tps/TB1xuUcNVXXXXcRXXXXXXXXXXXX-1000-300.jpg', text: 'Mobile Phone Taobao Skin Call' },
{ url: 'https://img.alicdn.com/tps/TB1ikP.NVXXXXaYXpXXXXXXXXXX-1000-300.jpg', text: 'Design Enabling Public Welfare' },
{ url: 'https://img.alicdn.com/tps/TB1s1_JNVXXXXbhaXXXXXXXXXXX-1000-300.jpg', text: 'Amoy Doll Design Competition' }
];
const itemNodes = slides.map((item, index) => <div key={index} className="slider-img-wrapper"><img src={item.url} alt={item.text} /></div>);
ReactDOM.render(<Slider>{itemNodes}</Slider>, mountNode);
.slider-img-wrapper img{
width: 100%;
}
在单图轮播的基础上,通过指定slidesToShow
属性值,可以同时进行多图轮播。可以通过 slidesToScroll
属性制定单次轮播图片的个数。
查看源码在线预览
import { Slider } from '@alifd/next';
const slides = [1, 2, 3, 4, 5, 6, 7, 8, 9].map(item => <div style={{width: '25%'}} key={item}><h4 className="h4">{item}</h4></div>);
ReactDOM.render(
<div style={{width: '600px'}}>
<div className="demo-item-title">slide one picture one at a time</div>
<Slider slidesToShow={4} arrowPosition="outer" dots={false} lazyLoad >
{slides}
</Slider>
<div className="demo-item-title">slide multiple picture one at a time</div>
<Slider slidesToShow={4} slidesToScroll={4} arrowPosition="outer" lazyLoad dots={false}>
{slides}
</Slider>
</div>
, mountNode);
.next-slick .h3, .h4 {
margin: 0 5px;
background: #4F74B3;
color: #fff;
line-height: 150px;
text-align: center;
margin-top: 0;
margin-bottom: 0;
}
.demo-item-title {
font-size: 16px;
color: #333;
padding: 8px;
margin: 20px 0 10px 0;
}
轮播组件可以通过 slideDirection
属性设置两种轮播方向。当值为 ver
时轮播方向为垂直方向,默认为值为 hoz
。垂直模式也可以设置单图和多图轮播。
查看源码在线预览
import { Slider } from '@alifd/next';
ReactDOM.render(<div style={{ width: '200px' }}>
<div className="demo-item-title">Vertical multi-picture mode</div>
<Slider slideDirection="ver" slidesToShow={3} slidesToScroll={1} dots={false} arrowPosition="inner" arrowDirection="ver" className="ver-slick">
{
[1, 2, 3, 4, 5].map((item, index) => <div key={index} className="custom-slider" style={{ border: '1px solid transparent'}}><h3 className="h3">{item}</h3></div>)
}
</Slider>
<div className="demo-item-title">Vertical single-picture mode</div>
<Slider slideDirection="ver" dots={false} arrowPosition="inner" arrowDirection="ver" className="ver-slick">
{
[1, 2, 3, 4, 5].map((item, index) => <div key={index} className="custom-slider"><h3 className="h3">{item}</h3></div>)
}
</Slider>
</div>, mountNode);
.ver-slick .h3 {
background: #4F74B3;
color: #fff;
line-height: 150px;
text-align: center;
font-size: 36px;
margin-top: 0;
margin-bottom: 0;
}
.demo-item-title {
font-size: 16px;
color: #333;
padding: 8px;
margin: 20px 0 10px 0;
}
可以通过arrowSize
属性来更改导航组件的按钮尺寸,默认值为normal
,对特定场景,比如展示的图片较大的情况下,可以选择large
,将导航按钮设置为大按钮。
查看源码在线预览
import { Slider } from '@alifd/next';
const bigSlides = [
{ url: 'https://img.alicdn.com/tps/TB1bewbNVXXXXc5XXXXXXXXXXXX-1000-300.png', text: 'Tape Player Skin Design Competition' },
{ url: 'https://img.alicdn.com/tps/TB1xuUcNVXXXXcRXXXXXXXXXXXX-1000-300.jpg', text: 'Mobile Phone Taobao Skin Call' },
{ url: 'https://img.alicdn.com/tps/TB1ikP.NVXXXXaYXpXXXXXXXXXX-1000-300.jpg', text: 'Design Enabling Public Welfare' },
{ url: 'https://img.alicdn.com/tps/TB1s1_JNVXXXXbhaXXXXXXXXXXX-1000-300.jpg', text: 'Amoy Doll Design Competition' }
];
ReactDOM.render(<div>
<Slider>
{
bigSlides.map((item, index) => <div key={index} className="slider-img-wrapper"><img src={item.url} alt={item.text} /></div>)
}
</Slider>
<br />
<Slider arrowSize="large">
{
bigSlides.map((item, index) => <div key={index} className="slider-img-wrapper"><img src={item.url} alt={item.text} /></div>)
}
</Slider>
</div>, mountNode);
.slider-img-wrapper img {
width: 100%;
}
通过 dotsDirection
可以改变导航锚点的位置,默认为 hoz
即水平方向。当其值设为 ver
时为垂直方向展示。
查看源码在线预览
import { Slider } from '@alifd/next';
ReactDOM.render(
<div>
<Slider dotsDirection="hoz" arrows={false}>
<div><h3 className="h3">1</h3></div>
<div><h3 className="h3">2</h3></div>
<div><h3 className="h3">3</h3></div>
<div><h3 className="h3">4</h3></div>
</Slider>
<br />
<Slider dotsDirection="ver" arrows={false}>
<div><h3 className="h3">1</h3></div>
<div><h3 className="h3">2</h3></div>
<div><h3 className="h3">3</h3></div>
<div><h3 className="h3">4</h3></div>
</Slider>
</div>
, mountNode);
.next-slick .h3, .h4 {
background: #4F74B3;
color: #fff;
line-height: 150px;
text-align: center;
margin-top: 0;
margin-bottom: 0;
}
.next-slick .h4 {
margin-right: 5px;
position: relative;
}
通过 triggerType
可以定义dots触发方式,共有两种触发方式:['click', 'hover'];当其值设为 hover
时为鼠标经过触发滚动。
查看源码在线预览
import { Slider } from '@alifd/next';
ReactDOM.render(
<div>
<Slider triggerType="click" arrows={false} >
<div><h3 className="h3">1</h3></div>
<div><h3 className="h3">2</h3></div>
<div><h3 className="h3">3</h3></div>
<div><h3 className="h3">4</h3></div>
</Slider>
<br/>
<Slider triggerType="hover" arrows={false} >
<div><h3 className="h3">1</h3></div>
<div><h3 className="h3">2</h3></div>
<div><h3 className="h3">3</h3></div>
<div><h3 className="h3">4</h3></div>
</Slider>
</div>
, mountNode);
.next-slick .h3, .h4 {
background: #4F74B3;
color: #fff;
line-height: 150px;
text-align: center;
margin-top: 0;
margin-bottom: 0;
}
.next-slick .h4 {
margin-right: 5px;
position: relative;
}
通过 dotsRender
可以自定义修改dost,通过 dotsClass
可覆盖dots的样式。
查看源码在线预览
import { Slider } from '@alifd/next';
ReactDOM.render(
<div>
<Slider dotsClass="dots-cust" dotsDirection="hoz" arrows={false} dotsRender={(index, current) => {
console.log('current', current);
return <a>{index}</a>;
}}>
<div><h3 className="h3">0</h3></div>
<div><h3 className="h3">1</h3></div>
<div><h3 className="h3">2</h3></div>
<div><h3 className="h3">3</h3></div>
<div><h3 className="h3">4</h3></div>
</Slider>
</div>
, mountNode);
.next-slick .h3, .h4 {
background: #4F74B3;
color: #fff;
line-height: 150px;
text-align: center;
margin-top: 0;
margin-bottom: 0;
}
.next-slick .h4 {
margin-right: 5px;
position: relative;
}
.dots-cust {
color: #fff;
}
.dots-cust a{
dispaly: block;
background: rgba(200,200,200,.4);
padding: 0 4px;
}
.dots-cust .active a{
color: rgb(70, 188, 2);
}
开发者可以通过 prevArrow
和 nextArrow
两个属性传入自定义的导航箭头组件。
查看源码在线预览
import { Slider, Icon } from '@alifd/next';
const slides = [
{ url: 'https://img.alicdn.com/tps/TB1bewbNVXXXXc5XXXXXXXXXXXX-1000-300.png', text: 'Tape Player Skin Design Competition' },
{ url: 'https://img.alicdn.com/tps/TB1xuUcNVXXXXcRXXXXXXXXXXXX-1000-300.jpg', text: 'Mobile Phone Taobao Skin Call' },
{ url: 'https://img.alicdn.com/tps/TB1ikP.NVXXXXaYXpXXXXXXXXXX-1000-300.jpg', text: 'Design Enabling Public Welfare' },
{ url: 'https://img.alicdn.com/tps/TB1s1_JNVXXXXbhaXXXXXXXXXXX-1000-300.jpg', text: 'Amoy Doll Design Competition' }
];
const arrowStyle = {
display: 'block',
background: 'red',
opacity: 1,
margin: '0 20px'
};
const CustomNextArrow = (props) => {
return <div {...props} style={arrowStyle}><Icon type="arrow-double-right" /></div>;
};
const CustomPrevArrow = (props) => {
return <div {...props} style={arrowStyle}><Icon type="arrow-double-left" /></div>;
};
ReactDOM.render(
<Slider nextArrow={<CustomNextArrow />} prevArrow={<CustomPrevArrow />} lazyLoad>
{
slides.map((item, index) => <div key={index} className="slider-img-wrapper"><img src={item.url} alt={item.text} /></div>)
}
</Slider>
, mountNode);
.slider-img-wrapper img {
width: 100%;
}
轮播组件的导航按钮在默认情况下为内置模式。在多图同时导航的情况下,如果想要使用外置按钮,可以通过指定arrowPosition
的属性值为outer
,使用外置按钮,其默认值为inner
。
查看源码在线预览
import { Slider } from '@alifd/next';
ReactDOM.render(
<div>
<Slider slidesToShow={4} arrowPosition="outer">
<div style={{width: '25%'}}><h4 className="h4">1</h4></div>
<div style={{width: '25%'}}><h4 className="h4">2</h4></div>
<div style={{width: '25%'}}><h4 className="h4">3</h4></div>
<div style={{width: '25%'}}><h4 className="h4">4</h4></div>
<div style={{width: '25%'}}><h4 className="h4">5</h4></div>
</Slider>
<br />
<Slider>
<div><h3 className="h3">1</h3></div>
<div><h3 className="h3">2</h3></div>
<div><h3 className="h3">3</h3></div>
<div><h3 className="h3">4</h3></div>
</Slider>
</div>
, mountNode);
.next-slick .h3, .h4 {
background: #4F74B3;
color: #fff;
line-height: 150px;
text-align: center;
margin-top: 0;
margin-bottom: 0;
}
.next-slick .h4 {
margin: 0 5px;
position: relative;
}
可以通过 autoplay
和 autoplaySpeed
属性来设置组件是否自动轮播 和 自动轮播的速度。
查看源码在线预览
import { Slider } from '@alifd/next';
ReactDOM.render(
<div style={{width: '600px'}}>
<Slider slidesToShow={4} arrowPosition="outer" lazyLoad dots={false} autoplay autoplaySpeed={1000}>
<div style={{width: '25%'}}><h4 className="h4">1</h4></div>
<div style={{width: '25%'}}><h4 className="h4">2</h4></div>
<div style={{width: '25%'}}><h4 className="h4">3</h4></div>
<div style={{width: '25%'}}><h4 className="h4">4</h4></div>
<div style={{width: '25%'}}><h4 className="h4">5</h4></div>
<div style={{width: '25%'}}><h4 className="h4">6</h4></div>
<div style={{width: '25%'}}><h4 className="h4">7</h4></div>
<div style={{width: '25%'}}><h4 className="h4">8</h4></div>
<div style={{width: '25%'}}><h4 className="h4">9</h4></div>
</Slider>
<br/>
<Slider speed={1000} autoplay autoplaySpeed={2000}>
<div style={{width: '25%'}}><h4 className="h4">1</h4></div>
<div style={{width: '25%'}}><h4 className="h4">2</h4></div>
<div style={{width: '25%'}}><h4 className="h4">3</h4></div>
</Slider>
</div>
, mountNode);
.next-slick .h3, .h4 {
margin: 0 5px;
background: #4F74B3;
color: #fff;
line-height: 150px;
text-align: center;
margin-top: 0;
margin-bottom: 0;
}
默认情况下,轮播组件的表现为无穷循环模式。如果你不想无穷循环,可以通过设置 infinite
为 false
,用来禁止循环模式。
值得注意的是,由于组件的默认行为是无穷模式,所以默认情况下,自动将单张图片复制了两份,如果你不想启用这样的复制效果,只要关闭 infinite
属性即可。
查看源码在线预览
import { Slider } from '@alifd/next';
ReactDOM.render(
<div>
<Slider slidesToShow={4} arrowPosition="outer" infinite={false} dots={false}>
<div style={{width: '25%'}}><h4 className="h4">1</h4></div>
<div style={{width: '25%'}}><h4 className="h4">2</h4></div>
<div style={{width: '25%'}}><h4 className="h4">3</h4></div>
<div style={{width: '25%'}}><h4 className="h4">4</h4></div>
<div style={{width: '25%'}}><h4 className="h4">5</h4></div>
</Slider>
<br />
<Slider infinite={false} lazyLoad>
<div><h3 className="h3">1</h3></div>
<div><h3 className="h3">2</h3></div>
<div><h3 className="h3">3</h3></div>
<div><h3 className="h3">4</h3></div>
</Slider>
</div>
, mountNode);
.next-slick .h3, .h4 {
background: #4F74B3;
color: #fff;
line-height: 150px;
text-align: center;
margin-top: 0;
margin-bottom: 0;
}
.next-slick .h4 {
margin: 0 5px;
position: relative;
}
可以通过设置 pauseOnHover
属性为 true
使得 Slide 在鼠标悬浮时自动停止轮播。
查看源码在线预览
import { Slider } from '@alifd/next';
const settings = {
className: 'custom-slide',
arrowPosition: 'outer',
dots: false,
infinite: true,
slidesToShow: 3,
slidesToScroll: 1,
autoplay: true,
autoplaySpeed: 2000,
pauseOnHover: true
};
ReactDOM.render(
<Slider {...settings}>
<div><h3>1</h3></div>
<div><h3>2</h3></div>
<div><h3>3</h3></div>
<div><h3>4</h3></div>
<div><h3>5</h3></div>
<div><h3>6</h3></div>
<div><h3>7</h3></div>
<div><h3>8</h3></div>
<div><h3>9</h3></div>
</Slider>
, mountNode);
.custom-slide h3 {
background: #4F74B3;
color: #fff;
font-size: 36px;
line-height: 100px;
margin: 10px;
padding: 2%;
position: relative;
text-align: center;
}
用户可以包装 Slider 组件,以便进行外部控制。例如通过包装组件实现外部对 Slider 组件 autoplay
和 autoplaySpeed
值的控制。
查看源码在线预览
import { Slider, Select } from '@alifd/next';
const { Option } = Select;
class SliderWrapper extends React.Component {
constructor(props) {
super(props);
this.state = {
autoplay: false,
autoplaySpeed: 1000
};
}
onSelectAutoplay(value) {
this.setState({ autoplay: value });
}
onSelectAutoplaySpeed(value) {
this.setState({ autoplaySpeed: value });
}
render() {
return (<div>
<Select placeholder="Autoplay" onChange={this.onSelectAutoplay.bind(this)}>
<Option value>True</Option>
<Option value={false}>False</Option>
</Select>
<Select placeholder="Autoplay Speed" onChange={this.onSelectAutoplaySpeed.bind(this)}>
<Option value={1000}>1 second</Option>
<Option value={2000}>2 seconds</Option>
<Option value={3000}>3 seconds</Option>
</Select>
<br />
<br />
<Slider autoplay={this.state.autoplay} autoplaySpeed={this.state.autoplaySpeed}>
<div><h3 className="h3">1</h3></div>
<div><h3 className="h3">2</h3></div>
<div><h3 className="h3">3</h3></div>
<div><h3 className="h3">4</h3></div>
</Slider>
</div>);
}
}
ReactDOM.render(<SliderWrapper />, mountNode);
.next-slick .h3, .h4 {
background: #4F74B3;
color: #fff;
line-height: 150px;
text-align: center;
margin-top: 0;
margin-bottom: 0;
}
居中模式可以突出显示最核心区域的内容,您可以通过设置 centerMode
属性址为 true
开启该功能。
查看源码在线预览
import { Slider } from '@alifd/next';
const settings = {
className: 'custom-slide center',
centerMode: true,
infinite: true,
dots: false,
arrowPosition: 'outer',
centerPadding: '60px',
slidesToShow: 3,
speed: 500
};
ReactDOM.render(
<Slider {...settings}>
<div><h3>1</h3></div>
<div><h3>2</h3></div>
<div><h3>3</h3></div>
<div><h3>4</h3></div>
<div><h3>5</h3></div>
<div><h3>6</h3></div>
<div><h3>7</h3></div>
<div><h3>8</h3></div>
<div><h3>9</h3></div>
</Slider>
, mountNode);
.custom-slide h3 {
background: #4F74B3;
color: #fff;
font-size: 36px;
line-height: 100px;
margin: 10px;
padding: 2%;
position: relative;
text-align: center;
}
.center h3 {
opacity: 0.8;
transition: all 300ms ease;
}
.center .next-slick-center h3 {
color: #e67e22;
opacity: 1;
transform: scale(1.08);
}
通过 index
属性可以快速的定位到指定次序的 slider 。
查看源码在线预览
import { Slider } from '@alifd/next';
const slides = [
{ url: 'https://img.alicdn.com/tps/TB1bewbNVXXXXc5XXXXXXXXXXXX-1000-300.png', text: 'Tape Player Skin Design Competition' },
{ url: 'https://img.alicdn.com/tps/TB1xuUcNVXXXXcRXXXXXXXXXXXX-1000-300.jpg', text: 'Mobile Phone Taobao Skin Call' },
{ url: 'https://img.alicdn.com/tps/TB1ikP.NVXXXXaYXpXXXXXXXXXX-1000-300.jpg', text: 'Design Enabling Public Welfare' },
{ url: 'https://img.alicdn.com/tps/TB1s1_JNVXXXXbhaXXXXXXXXXXX-1000-300.jpg', text: 'Amoy Doll Design Competition' }
];
class SlickGoTo extends React.Component {
constructor(props) {
super(props);
this.state = {
index: 0 // The initial value here need to be set to 0 for `activeIndex`. If you want the initial as 0 , you can use the `defaultActiveIndex` property to set.
};
this.changeHandler = this.changeHandler.bind(this);
}
changeHandler(e) {
this.setState({
index: parseInt(e.target.value)
});
}
render() {
const settings = {
dots: false,
infinite: true,
activeIndex: this.state.index
};
return (
<div>
<div className="demo-item-title">Pull the scroll bar to switch:</div>
<input onChange={this.changeHandler} defaultValue={0} type="range" min={0} max={3} />
<Slider {...settings}>
{
slides.map((item, index) => <div key={index} className="slider-img-wrapper"><img src={item.url} alt={item.text} /></div>)
}
</Slider>
</div>
);
}
}
ReactDOM.render(<SlickGoTo />, mountNode);
.slider-img-wrapper img {
width: 100%;
}
.demo-item-title {
font-size: 16px;
color: #333;
padding: 8px;
margin: 20px 0 10px 0;
}
Slider 在默认情况下会认为所有的子元素是等宽的。通过设置 variableWidth
为 true
,您可以在 Slider 中放置不同宽度的图片。
查看源码在线预览
import { Slider } from '@alifd/next';
const settings = {
className: 'custom-slide variable-width',
arrowPosition: 'outer',
dots: true,
infinite: true,
slidesToShow: 1,
slidesToScroll: 1,
variableWidth: true
};
ReactDOM.render(
<Slider {...settings}>
<div style={{width: 100}}><p>100</p></div>
<div style={{width: 200}}><p>200</p></div>
<div style={{width: 75}}><p>75</p></div>
<div style={{width: 300}}><p>300</p></div>
<div style={{width: 225}}><p>225</p></div>
<div style={{width: 175}}><p>175</p></div>
</Slider>
, mountNode);
.variable-width .next-slick-slide p {
background: #4F74B3;;
height: 100px;
color: #fff;
margin: 5px;
line-height: 100px;
text-align: center;
}
你可以为传递自定义组件到 Slider 组件中。前提是该组件一定要开放透传 props 到下层组件或元素,Slider 底层需要执行元素的 clone 操作。
查看源码在线预览
import { Slider } from '@alifd/next';
import PropTypes from 'prop-types';
const pages = [1, 2, 3, 4];
function Inner({ children, ...others }) {
// Note that to transparently pass other attributes to the lower node, the Slider needs to perform the element's clone operation.
return (
<div {...others}>
{children}
</div>
);
}
Inner.propTypes = {
children: PropTypes.any
};
const slider = (<Slider>
{
pages.map((page, index) => {
return <Inner className="custom-inner" key={index}>custom {page}</Inner>;
})
}
</Slider>);
ReactDOM.render(<div>
{slider}
</div>
, mountNode);
.custom-inner {
background: #4F74B3;
color: #fff;
line-height: 150px;
text-align: center;
margin-top: 0;
margin-bottom: 0;
}
如果你要将 Slider 放到 Dialog 中,此时你需要关闭 Dialog 的动画,避免 Slider 在计算内部元素宽度时造成出错。
查看源码在线预览
import { Slider, Dialog, Button } from '@alifd/next';
import PropTypes from 'prop-types';
const slides = [
{ url: 'https://img.alicdn.com/tps/TB1bewbNVXXXXc5XXXXXXXXXXXX-1000-300.png', text: 'Tape Player Skin Design Competition' },
{ url: 'https://img.alicdn.com/tps/TB1xuUcNVXXXXcRXXXXXXXXXXXX-1000-300.jpg', text: 'Mobile Phone Taobao Skin Call' },
{ url: 'https://img.alicdn.com/tps/TB1ikP.NVXXXXaYXpXXXXXXXXXX-1000-300.jpg', text: 'Design Enabling Public Welfare' },
{ url: 'https://img.alicdn.com/tps/TB1s1_JNVXXXXbhaXXXXXXXXXXX-1000-300.jpg', text: 'Amoy Doll Design Competition' }
];
const dialogStyle = {
width: '800px'
};
class FlappySlider extends React.Component {
static propTypes = {
slides: PropTypes.array
}
constructor(props) {
super(props);
this.state = {
visible: false
};
this.setVisible = this.setVisible.bind(this);
}
setVisible() {
this.setState(prevState => {
return {
visible: !prevState.visible
};
});
}
render() {
return (<div className="demo-wrapper">
<Button type="primary" onClick={this.setVisible}>Marquee Banner</Button>
<Dialog visible={this.state.visible} title="Alibaba.com" footer={false} style={dialogStyle} animation={false} onClose={this.setVisible}>
<Slider>
{
this.props.slides.map((item, index) => <div key={index} className="slider-img-wrapper"><img key={index} src={item.url} alt={item.text} /></div>)
}
</Slider>
</Dialog>
</div>);
}
}
ReactDOM.render(<FlappySlider slides={slides}/>, mountNode);
.slider-img-wrapper img {
width: 100%;
}
切换跑马灯时使用渐变效果。
查看源码在线预览
import { Slider } from '@alifd/next';
const settings = {
arrowPosition: 'outer',
dots: false,
animation: 'fade',
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1,
className: 'custom-slide',
onChange: function (index) {
console.log('change Slide index', index);
}
};
ReactDOM.render(
<div>
<Slider {...settings}>
{[1, 2, 3, 4, 5, 6].map(function(d) {
return <div key={d}><h3 onClick={() => console.log(d)} >{d}</h3></div>;
})}
</Slider>
<Slider {...settings}>
{[1, 2, 3, 4, 5, 6].map(function(d) {
return <div key={d}><a href={`https://www.taobao.com/?some=${d}`} target="_blank">{d}</a></div>;
})}
</Slider>
</div>
, mountNode
);
.custom-slide h3, .custom-slide a {
display: block;
background: #4F74B3;
color: #fff;
font-size: 36px;
line-height: 100px;
margin: 10px;
padding: 2%;
position: relative;
text-align: center;
}
你可以利用 onChange
钩子函数处理一些额外的逻辑。
查看源码在线预览
import { Slider } from '@alifd/next';
const settings = {
className: 'custom-slide',
arrowPosition: 'outer',
dots: false,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1,
onChange: function (index) {
console.log('change Slide index', index);
}
};
ReactDOM.render(
<Slider {...settings}>
<div><h3>1</h3></div>
<div><h3>2</h3></div>
<div><h3>3</h3></div>
<div><h3>4</h3></div>
<div><h3>5</h3></div>
<div><h3>6</h3></div>
<div><h3>7</h3></div>
<div><h3>8</h3></div>
<div><h3>9</h3></div>
</Slider>
, mountNode);
.custom-slide h3 {
background: #4F74B3;
color: #fff;
font-size: 36px;
line-height: 100px;
margin: 10px;
padding: 2%;
position: relative;
text-align: center;
}