JavaScript实现多图片跳转

利用函数setInterval()函数,实现图片循环的代码。
同时实现点击跳转到不同的页面。

本来挺简单的一个问题,被我想太复杂了,耗了长时间没有想出来。卡在switch上……

switch(n)
{
case 1:
执行代码块 1
break;
case 2:
执行代码块 2
break;
default:
n 与 case 1 和 case 2 不同时执行的代码
}

n为表达式,表达式的结果必须时整数、字符或是枚举量。

自己错误的用了各种网站的url作为常量,与表达式对应。


下面列上程序:
script部分:

<script>
var banner = ["img/0.jpg","img/1.jpg","img/tz.jpg"];
var counter = 0;
function run(){
setInterval(cycle,2000);
}
function cycle() {
counter++;
if (counter == banner.length) counter = 0;
document.getElementById("banner").src = banner[counter];
}
function page() {
if(counter==0)
window.location.href = 'https://www.baidu.com';
else if(counter==1)
window.location.href = 'https://wflorisgu.github.io/';
else if(counter==2)
window.location.href = 'http://202.118.201.228/homepage/index.do';
}
</script>

body部分:

<body onload="run()" >
<img id ="banner" alt="banner" src="img/0.jpg" onclick="page()"/>

猜你喜欢

转载自blog.csdn.net/floris_lovelace/article/details/80411983