请马上登录,朋友们都在花潮里等着你哦:)
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码:
<style>
/* 应根据需要设置好图片的父容器尺寸,所准备的图片宽高比例应大致一致 */
#tz { margin: auto; width: 460px; height: 640px; }
#mypic { width: 100%; height: 100%; }
</style>
<div id="tz">
<img id="mypic" src="https://n.sinaimg.cn/sinakd20118/500/w2000h2500/20200618/4de3-ivffpcs2449712.jpg" />
</div>
<p><button id="btnCtrl" type="button" value="controls">播放图片</button></p>
<script>
var picIdx = 0, picPlay = true, pTimer = null; //图片索引、是否播放、计时器标识
var picAr = [
'图片1',
'图片2',
'图片3',
'图片4',
];
var showPics = (pics, time = 5000) => {
picIdx = (picIdx + 1) % pics.length;
mypic.src = pics[picIdx];
pTimer = setTimeout( () => {
showPics(pics, time);
}, time);
};
//按钮控制图片只是为了演示,实际使用时可以考虑与其他布尔变量联动
btnCtrl.onclick = () => {
if(picPlay) {
showPics(picAr, 3000);
btnCtrl.textContent = '暂停播放';
}else{
clearTimeout(pTimer);
btnCtrl.textContent = '播放图片';
}
picPlay = !picPlay;
};
</script>
|