- 代码
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
#big-main{
width: 100px;
height: 200px;
}
img{
max-width: 100%;
max-height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="big-main">
<img class="img-box" src="image/SunFlower0001.png" alt="">
</div>
</body>
<script>
const config = {
kcid: 'kui',
name: '向日葵',
image: 'image/SunFlower0001.png',
zSize: 25,
scount: 100,
imgClass: '.img-box'
}
setInterval(()=>{
changeImg(config)
}, config.scount)
function changeImg(item){
const block = item.image.indexOf('.')
const beforeSrc = item.image.slice(0, block-4)
const afterSrc = item.image.slice(block)
let centerSrc = item.image.slice(block-4, block)
let count = Number(centerSrc)+1
if(count>item.zSize){
count = 1
}
centerSrc = String(count)
if(centerSrc.length==1){
centerSrc = '000'+centerSrc
}
if(centerSrc.length==2){
centerSrc = '00'+centerSrc
}
if(centerSrc.length==3){
centerSrc = '0'+centerSrc
}
config.image = beforeSrc+ centerSrc + afterSrc
console.log('当前图片路径', config.image)
document.querySelector(item.imgClass).setAttribute('src', config.image)
}
</script>
</html>
- 图片资源如下25张