首页 > 代码库 > js制作带按钮的循环自动切换广告
js制作带按钮的循环自动切换广告
-----------------------------------------------------------------------------------------------------------------css
@charset "gb2312";
/* CSS Document */
body{
margin:0;
margin-top:3px;
padding:0;
font-size:12px;
line-height:20px;
color:#333;
}
.adver{
width:426px;
border:solid 1px #666;
margin-left:auto;
margin-right:auto;
}
.Rotator_bg{
background-color:#E0E0E0;
height:20px;
border-top:solid 1px #B4B4B4;
}
.number{
font-size: 14px;
font-weight: bold;
color: #FFF;
background-color: #9E2E07;
display: block;
border: 1px solid #FFF;
width:18px;
height:18px;
text-align: center;
margin-left:10px;
cursor:pointer;
float:left;
}
.numberOver{
color:#8C2806;
font-size:14px;
width:280px;
background-color:#FFF;
text-align:center;
float:left;
display: block;
margin-left:10px;
}
.main{
width:95%;
margin-left:auto;
margin-right:auto;
}
.left_indent{
padding-left:20px;
}
.red{
color:#F00;
}
-----------------------------------------------------------------------------------------------------------------js
// JavaScript Document
//定义全局变量
var title=new Array();
title[0]="1.店庆第一波 限时抢购 一日三疯!";
title[1]="2.十年店庆均价场 39/99/169专场!";
title[2]="3.全场69折封顶 享当当的超值低价!";
title[3]="4.店庆钜献 海量图书69折封顶";
title[4]="5.十年店庆 名家跨界祝贺 珍藏签名本专区";
var NowFrame = 1; //最先显示第一张图片
var MaxFrame = 5; //一共五张图片
function show(d1) {
if(Number(d1)){
clearTimeout(theTimer); //当触动按扭时,清除计时器
NowFrame=d1; //设当前显示图片
}
for(var i=1;i<(MaxFrame+1);i++){
if(i==NowFrame){
document.getElementById("Rotator").src ="http://www.mamicode.com/images/adRotator_"+i+".jpg"; //显示当前图片
document.getElementById("fig_"+i).innerHTML=title[i-1]; //显示当前图片对应的标题
document.getElementById("fig_"+i).className="numberOver"; //设置当前标题的CSS样式
}
else{
document.getElementById("fig_"+i).innerHTML=i;
document.getElementById("fig_"+i).className="number";
}
}
if(NowFrame == MaxFrame){ //设置下一个显示的图片
NowFrame = 1;
}
else{
NowFrame++;
}
}
var theTimer=setInterval(‘show()‘, 3000); //设置定时器,显示下一张图片
window.onload=show; //页面加载时运行函数show()
-----------------------------------------------------------------------------------------------------------------网页代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>带按钮的横幅广告</title>
<link href="http://www.mamicode.com/css/adRotator.css" rel="stylesheet" type="text/css" />
</head>
<body>
<!--带按钮的横幅广告开始-->
<div class="adver"><img src="http://www.mamicode.com/images/adRotator_1.jpg" alt="广告图片" id="Rotator"/>
<div class="Rotator_bg">
<div class="number" id="fig_1" onclick="show(1);">1</div>
<div class="number" id="fig_2" onclick="show(2);">2</div>
<div class="number" id="fig_3" onclick="show(3);">3</div>
<div class="number" id="fig_4" onclick="show(4);">4</div>
<div class="number" id="fig_5" onclick="show(5);">5</div>
</div>
</div>
<script src="http://www.mamicode.com/js/adRotator.js" type="text/javascript"></script>
</body>
</html>
js制作带按钮的循环自动切换广告