js定时器
作者:互联网
<script type="text/javascript">
// 1.显示图片
class ShowPic{
imgList
constructor(){
this.imgList = document.querySelector('.imgList')
}
setNum(n){
// 0 1 2 3
// 0% 25% 50% 75%
let per = n/4*100
this.imgList.style.transform = `translateX(-${per}%)`
}
}
let show = new ShowPic()
// 2.控制i的值
class Index{
i = 0
inc(){
this.i++
if (this.i>3) {
this.i = 0
}
return this.i
}
dec(){
this.i--
if (this.i<0) {
this.i = 3
}
return this.i
}
}
let index = new Index()
// 3.定时器.开启,停止
class Time{
timeId
start(){
this.timeId = setInterval(()=>{
show.setNum( index.inc() )
},2000)
}
stop(){
clearInterval(this.timeId)
}
}
let time = new Time()
//
time.start()
</script>
标签:定时器,show,imgList,js,timeId,let,new,class 来源: https://blog.csdn.net/weixin_51958493/article/details/113810442