Slider 图片轮播
如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
npm install @icedesign/base@latest -S
Guide
轮播组件,就是以幻灯片的方式,在页面中横向展示诸多内容的组件。轮播内容相互独立,前后在内容以及数据上都不存在逻辑关系。
何时使用
单图轮播:该样式通常用于承载运营banner,是一个位置相对固定的模块。
多图轮播:单元信息浏览
使用注意点
当轮播组件中只有一张图片的时候,轮播组件会隐藏导航锚点、禁止自动循环(即使上层设置了)、禁止拖拽播放(即使上层设置了)。
如果您要将 Slider 放到 Dialog 中,此时你需要关闭 Dialog 的动画,避免 Slider 在计算内部元素宽度时造成出错。
如果出现图片高度 1px 的问题,可以尝试在
img
标签的外部包裹一层div
标签。
API
图片轮播
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 样式前缀 | String | 'next-' |
className | 自定义传入的样式 | String | - |
adaptiveHeight | 是否使用自适应高度 | Boolean | false |
animation | 是否开启动效 | Boolean | true |
arrows | 是否显示箭头 | Boolean | true |
arrowSize | 导航箭头大小可选值:'medium', 'large' | Enum | 'medium' |
arrowPos | 导航箭头位置可选值:'inline', 'outer' | Enum | 'inline' |
arrowDirection | 导航箭头的方向可选值:'horizontal', 'vertical' | Enum | 'horizontal' |
nextArrow | 向后箭头 | ReactElement | null |
prevArrow | 向后箭头 | ReactElement | null |
autoplay | 是否自动播放 | Boolean | false |
autoplaySpeed | 自动播放的速度 | Number | 3000 |
centerMode | 是否启用居中模式 | Boolean | false |
dots | 是否显示导航锚点 | Boolean | true |
dotsDirection | 导航锚点到位置可选值:'horizontal', 'vertical' | Enum | 'horizontal' |
draggable | 是否可拖拽 | Boolean | true |
fade | 是否使用淡入淡出效果 | Boolean | false |
infinite | 是否使用无穷循环模式 | Boolean | true |
initialSlide | 初始被激活的轮播图 | Number | 0 |
lazyLoad | 是否启用懒加载 | Boolean | false |
slideDirection | 轮播方向可选值:'horizontal', 'vertical' | Enum | 'horizontal' |
slidesToShow | 同时展示的图片数量 | Number | 1 |
slidesToScroll | 同时滑动到图片数量 | Number | 1 |
speed | 轮播速度 | Number | 500 |
slickGoTo | 跳转到指定的轮播图(受控) | Number | - |
afterChange | 轮播切换后的回调函数签名:Function(index: Number) => void参数:index: {Number} 当前幻灯片的索引 | Function | null |
beforeChange | 轮播切换前的回调函数签名:Function(currentIndex: Number, nextIndex: Number) => void参数:currentIndex: {Number} 当前幻灯片的索引nextIndex: {Number} 下一张幻灯片的索引 | Function | null |
代码示例
可以通过指定arrowPos
的属性值为outer
,使用外置按钮,其默认值为inline
。轮播组件的导航按钮在默认情况下为内置模式。在多图同时导航的情况下,如果想要使用外置按钮,
查看源码在线预览
import { Slider } from "@icedesign/base";
ReactDOM.render(
<div>
<Slider slidesToShow={4} arrowPos="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 "@icedesign/base";
ReactDOM.render(
<div style={{ width: "600px" }}>
<Slider
slidesToShow={4}
arrowPos="outer"
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>
</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;
}
注意: 如果出现图片 1px 高度的问题,建议将图片的外部包括一层 div
来避免这个问题。在默认模式下(不指定任何属性值),轮播组件为单图轮播模式。轮播组件共有两种类型:单图轮播和多图同时轮播。
查看源码在线预览
import { Slider } from "@icedesign/base";
const slides = [
{
url: "https://img.alicdn.com/tps/TB1bewbNVXXXXc5XXXXXXXXXXXX-1000-300.png",
text: "手机淘宝皮肤征集"
},
{
url: "https://img.alicdn.com/tps/TB1xuUcNVXXXXcRXXXXXXXXXXXX-1000-300.jpg",
text: "阿里公益T恤设计大概"
},
{
url: "https://img.alicdn.com/tps/TB1ikP.NVXXXXaYXpXXXXXXXXXX-1000-300.jpg",
text: "淘公仔设计大赛"
},
{
url: "https://img.alicdn.com/tps/TB1s1_JNVXXXXbhaXXXXXXXXXXX-1000-300.jpg",
text: "磁带播放器换肤设计大赛"
}
];
ReactDOM.render(
<Slider>
{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%;
}
对特定场景,比如展示的图片较大的情况下,可以选择large
,将导航按钮设置为大按钮。可以通过arrowSize
属性来更改导航组件的按钮尺寸,默认值为normal
,
查看源码在线预览
import { Slider } from "@icedesign/base";
const bigSlides = [
{
url: "https://img.alicdn.com/tps/TB1bewbNVXXXXc5XXXXXXXXXXXX-1000-300.png",
text: "磁带播放器换肤设计大赛"
},
{
url: "https://img.alicdn.com/tps/TB1xuUcNVXXXXcRXXXXXXXXXXXX-1000-300.jpg",
text: "手机淘宝皮肤征集"
},
{
url: "https://img.alicdn.com/tps/TB1ikP.NVXXXXaYXpXXXXXXXXXX-1000-300.jpg",
text: "设计赋能公益"
},
{
url: "https://img.alicdn.com/tps/TB1s1_JNVXXXXbhaXXXXXXXXXXX-1000-300.jpg",
text: "淘公仔设计大赛"
}
];
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%;
}
居中模式可以突出显示最核心区域的内容,您可以通过设置 centerMode
属性址为 true
开启该功能。
查看源码在线预览
import { Slider } from "@icedesign/base";
const settings = {
className: "custom-slide center",
centerMode: true,
infinite: true,
dots: false,
arrowPos: "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);
}
开发者可以通过 prevArrow
和 nextArrow
两个属性传入自定义的导航箭头组件。
查看源码在线预览
import { Slider, Icon } from "@icedesign/base";
const slides = [
{
url: "https://img.alicdn.com/tps/TB1bewbNVXXXXc5XXXXXXXXXXXX-1000-300.png",
text: "手机淘宝皮肤征集"
},
{
url: "https://img.alicdn.com/tps/TB1xuUcNVXXXXcRXXXXXXXXXXXX-1000-300.jpg",
text: "阿里公益T恤设计大概"
},
{
url: "https://img.alicdn.com/tps/TB1ikP.NVXXXXaYXpXXXXXXXXXX-1000-300.jpg",
text: "淘公仔设计大赛"
},
{
url: "https://img.alicdn.com/tps/TB1s1_JNVXXXXbhaXXXXXXXXXXX-1000-300.jpg",
text: "磁带播放器换肤设计大赛"
}
];
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%;
}
你可以为传递自定义组件到 Slider 组件中。前提是该组件一定要开放透传 props 到下层组件或元素,Slider 底层需要执行元素的 clone 操作。
查看源码在线预览
import { Slider } from "@icedesign/base";
import PropTypes from "prop-types";
const pages = [1, 2, 3, 4];
function Inner({ children, ...others }) {
// 注意这里要透传其他属性到到下层节点上,Slider 底层需要执行元素的 clone 操作
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;
}
当其值设为 vertical
时为垂直方向展示。通过 dotsDirection
可以改变导航锚点的位置,默认为 horizontal
即水平方向。
查看源码在线预览
import { Slider } from "@icedesign/base";
ReactDOM.render(
<div>
<Slider dotsDirection="horizontal" 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="vertical" 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;
}
切换跑马灯时使用渐变效果。
查看源码在线预览
import { Slider } from "@icedesign/base";
const settings = {
arrowPos: "outer",
dots: false,
fade: true,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1,
className: "custom-slide"
};
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;
}
如果你不想启用这样的复制效果,只要关闭 infinite
属性即可。值得注意的是,由于组件的默认行为是无穷模式,所以默认情况下,自动将单张图片复制了两份,可以通过设置 infinite
为 false
,用来禁止循环模式。默认情况下,轮播组件的表现为无穷循环模式。如果你不想无穷循环,
查看源码在线预览
import { Slider } from "@icedesign/base";
ReactDOM.render(
<div>
<Slider slidesToShow={4} arrowPos="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}>
<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;
}
可以通过 slidesToScroll
属性制定单次轮播图片的个数。在单图轮播的基础上,通过指定slidesToShow
属性值,可以同时进行多图轮播。
查看源码在线预览
import { Slider } from "@icedesign/base";
const slides = [1, 2, 3, 4, 5, 6, 7, 8, 9].map(item => (
<div style={{ width: "25%" }} key={item}>
<h4 className="h4">1</h4>
</div>
));
ReactDOM.render(
<div style={{ width: "600px" }}>
<div className="demo-item-title">一次轮播一张图片</div>
<Slider slidesToShow={4} arrowPos="outer" dots={false}>
{slides}
</Slider>
<div className="demo-item-title">一次轮播多张图片</div>
<Slider slidesToShow={4} slidesToScroll={4} arrowPos="outer" 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;
}
可以通过设置 pauseOnHover
属性为 true
使得 Slide 在鼠标悬浮时自动停止轮播。
查看源码在线预览
import { Slider } from "@icedesign/base";
const settings = {
className: "custom-slide",
arrowPos: "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;
}
通过 slickGoTo
方法可以快速的定位到指定次序的 slider 。
查看源码在线预览
import { Slider } from "@icedesign/base";
const slides = [
{
url: "https://img.alicdn.com/tps/TB1bewbNVXXXXc5XXXXXXXXXXXX-1000-300.png",
text: "磁带播放器换肤设计大赛"
},
{
url: "https://img.alicdn.com/tps/TB1xuUcNVXXXXcRXXXXXXXXXXXX-1000-300.jpg",
text: "手机淘宝皮肤征集"
},
{
url: "https://img.alicdn.com/tps/TB1ikP.NVXXXXaYXpXXXXXXXXXX-1000-300.jpg",
text: "设计赋能公益"
},
{
url: "https://img.alicdn.com/tps/TB1s1_JNVXXXXbhaXXXXXXXXXXX-1000-300.jpg",
text: "淘公仔设计大赛"
}
];
class SlickGoTo extends React.Component {
constructor(props) {
super(props);
this.state = {
currentSlide: 0 // 这里初始值需要置为 0,如果你希望初始值不为 0 ,可以使用 initialSlide 属性设置
};
this.changeHandler = this.changeHandler.bind(this);
}
changeHandler(e) {
this.setState({
currentSlide: parseInt(e.target.value)
});
}
render() {
const settings = {
dots: false,
infinite: true,
slickGoTo: this.state.currentSlide
};
return (
<div>
<div className="demo-item-title">拉动下面的滚动条切换</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;
}
你可以利用 beforeChange
和 afterChange
两个钩子函数处理一些额外的逻辑。
查看源码在线预览
import { Slider } from "@icedesign/base";
const settings = {
className: "custom-slide",
arrowPos: "outer",
dots: false,
infinite: true,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1,
beforeChange: function(currentSlide, nextSlide) {
console.log("before change", currentSlide, nextSlide);
},
afterChange: function(currentSlide) {
console.log("after change", currentSlide);
}
};
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;
}
默认为值为 horizontal
。垂直模式也可以设置单图和多图轮播。轮播组件可以通过 slideDirection
属性设置两种轮播方向。当值为 vertical
时轮播方向为垂直方向,
查看源码在线预览
import { Slider } from "@icedesign/base";
ReactDOM.render(
<div style={{ width: "200px" }}>
<div className="demo-item-title">垂直多图模式</div>
<Slider
slideDirection="vertical"
slidesToShow={3}
slidesToScroll={1}
dots={false}
arrowPos="inline"
arrowDirection="vertical"
className="vertical-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">垂直单图模式</div>
<Slider
slideDirection="vertical"
dots={false}
arrowPos="inline"
arrowDirection="vertical"
className="vertical-slick"
>
{[1, 2, 3, 4, 5].map((item, index) => (
<div key={index} className="custom-slider">
<h3 className="h3">{item}</h3>
</div>
))}
</Slider>
</div>,
mountNode
);
.vertical-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;
}
如果你要将 Slider 放到 Dialog 中,此时你需要关闭 Dialog 的动画,避免 Slider 在计算内部元素宽度时造成出错。
查看源码在线预览
import { Slider, Dialog, Button } from "@icedesign/base";
import PropTypes from "prop-types";
const slides = [
{
url: "https://img.alicdn.com/tps/TB1bewbNVXXXXc5XXXXXXXXXXXX-1000-300.png",
text: "磁带播放器换肤设计大赛"
},
{
url: "https://img.alicdn.com/tps/TB1xuUcNVXXXXcRXXXXXXXXXXXX-1000-300.jpg",
text: "手机淘宝皮肤征集"
},
{
url: "https://img.alicdn.com/tps/TB1ikP.NVXXXXaYXpXXXXXXXXXX-1000-300.jpg",
text: "设计赋能公益"
},
{
url: "https://img.alicdn.com/tps/TB1s1_JNVXXXXbhaXXXXXXXXXXX-1000-300.jpg",
text: "淘公仔设计大赛"
}
];
const dialogStyle = {
width: "800px"
};
class FlappySlider extends React.Component {
static propTypes = {
slides: PropTypes.array
};
constructor(props) {
super(props);
this.state = {
visible: false
};
}
setVisible() {
this.setState(prevState => {
return {
visible: !prevState.visible
};
});
}
render() {
return (
<div className="demo-wrapper">
<Button type="primary" onClick={this.setVisible.bind(this)}>
显示跑马灯
</Button>
<Dialog
visible={this.state.visible}
title="Alibaba.com"
footer={false}
style={dialogStyle}
animation={false}
onClose={this.setVisible.bind(this)}
>
<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%;
}
用户可以包装 Slider 组件,以便进行外部控制。例如通过包装组件实现外部对 Slider 组件 autoplay
和 autoplaySpeed
值的控制。
查看源码在线预览
import { Slider, Select } from "@icedesign/base";
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="自动播放"
onChange={this.onSelectAutoplay.bind(this)}
>
<Option value>True</Option>
<Option value={false}>False</Option>
</Select>
<Select
placeholder="轮播速度"
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;
}
通过设置 variableWidth
为 true
,您可以在 Slider 中放置不同宽度的图片。Slider 在默认情况下会认为所有的子元素是等宽的。
查看源码在线预览
import { Slider } from "@icedesign/base";
const settings = {
className: "custom-slide variable-width",
arrowPos: "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;
}