export default class Lunbo extends React.Component<any,any>{
constructor(props:any){
super(props);
this.state = {
locations: [“left100”, “left100”, “left100”, “left100”, “left100”],
index:0,
timer:null,
locationss: [“left0”, “left100”, “left100”, “left100”, “left100”]
}
}
componentDidMount() {
this.l()
}
componentWillUnmount(){
clearInterval(this.state.timer)
}
l() {
const timer = setInterval(() => {
let {index, locations} = this.state
let _data = locations
if(index >= _data.length - 1) {
this.setState({index: 0})
}else{
this.setState({index: index + 1})
}
console.log(this.state.index)
_data[this.state.index] = "left0"
if(this.state.index + 1 > _data.length - 1){
_data[0] = "left100"
}else{
_data[this.state.index + 1] = "left100"
}
if(this.state.index <= 0){
_data[_data.length - 1] = "left_100"
}else{
_data[this.state.index - 1] = "left_100"
}
this.setState({locationss: _data})
},2000)
this.setState({timer})
}
render() {
let {locations, locationss} = this.state
return (
<>
<ul className="home_view_ul">
{
locationss.map((v:string, i:number) => {
return <li key={"KS"+i} className={v}>{i}</li>
})
}
</ul>
</>
)
}
}
本文介绍了如何使用React创建一个简单的轮播组件。通过state管理位置状态,并在构造函数中初始化,实现轮播图片的切换效果。
1154

被折叠的 条评论
为什么被折叠?



