在 React 中根据用户偏好动态更改 React-slick 滑块点图标
change react-slick slider Dots icon dynamically with user preference in React
嗨,我有一些使用 react-slick 滑块的自定义。
我有两个 png 点图标,它可以动态更改(用户可以从管理部分和 return 前端更改为 API 数据)
../navigator.png
and
../navigator-active.png
sliderSettings = {
dots: true,
infinite: true,
slidesToShow: 1,
slidesToScroll: 1,
autoplay: true,
appendDots: dots => {
return <ul style={{ margin: '0px' }}>{dots}</ul>;
},
customPaging: (pagi, i) => {
const style = {
width: 13,
height: 13,
display: 'inline-block',
backgroundImage: `url(../navigator.png )`, // need to change as navigator-active.png this when active
backgroundSize: 'contain',
backgroundRepeat: 'no-repeat',
};
return <a className="slick-dot" style={style} />;
},
};
是否可以添加活动图标,需要将默认点图标更改为活动点图标
你可以试试这个,只需要用setState记录当前的silde索引,并根据它改变样式。
render() {
var settings = {
dots: true,
infinite: true,
slidesToShow: 1,
slidesToScroll: 1,
autoplay: true,
appendDots: dots => {
return <ul style={{ margin: "0px" }}>{dots}</ul>;
},
beforeChange: (prev, next) => { // here to detect slide change
this.setState({ currentSlideIndex: next });
},
customPaging: (pagi, i) => {
const style = {
width: 13,
height: 13,
display: "inline-block",
backgroundImage: `url(../navigator.png )`, // need to change as navigator-active.png this when active
backgroundSize: 'contain',
backgroundRepeat: "no-repeat"
};
const activeStyle = {
width: 13,
height: 13,
display: "inline-block",
backgroundImage: `url(../navigator-active.png )`,
backgroundSize: 'contain',
backgroundRepeat: "no-repeat"
};
return (
<a
className="slick-dot"
style={pagi === this.state.currentSlideIndex ? activeStyle : style}
/>
);
}
};
嗨,我有一些使用 react-slick 滑块的自定义。
我有两个 png 点图标,它可以动态更改(用户可以从管理部分和 return 前端更改为 API 数据)
../navigator.png
and
../navigator-active.png
sliderSettings = {
dots: true,
infinite: true,
slidesToShow: 1,
slidesToScroll: 1,
autoplay: true,
appendDots: dots => {
return <ul style={{ margin: '0px' }}>{dots}</ul>;
},
customPaging: (pagi, i) => {
const style = {
width: 13,
height: 13,
display: 'inline-block',
backgroundImage: `url(../navigator.png )`, // need to change as navigator-active.png this when active
backgroundSize: 'contain',
backgroundRepeat: 'no-repeat',
};
return <a className="slick-dot" style={style} />;
},
};
是否可以添加活动图标,需要将默认点图标更改为活动点图标
你可以试试这个,只需要用setState记录当前的silde索引,并根据它改变样式。
render() {
var settings = {
dots: true,
infinite: true,
slidesToShow: 1,
slidesToScroll: 1,
autoplay: true,
appendDots: dots => {
return <ul style={{ margin: "0px" }}>{dots}</ul>;
},
beforeChange: (prev, next) => { // here to detect slide change
this.setState({ currentSlideIndex: next });
},
customPaging: (pagi, i) => {
const style = {
width: 13,
height: 13,
display: "inline-block",
backgroundImage: `url(../navigator.png )`, // need to change as navigator-active.png this when active
backgroundSize: 'contain',
backgroundRepeat: "no-repeat"
};
const activeStyle = {
width: 13,
height: 13,
display: "inline-block",
backgroundImage: `url(../navigator-active.png )`,
backgroundSize: 'contain',
backgroundRepeat: "no-repeat"
};
return (
<a
className="slick-dot"
style={pagi === this.state.currentSlideIndex ? activeStyle : style}
/>
);
}
};