js实现自动轮播图以及数字按钮组

自动切换图片、数字按钮组及数字按钮click事件

该代码来自《web标准网页设计原理与制作技术》该书籍

完整代码如下:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="{CHARSET}">
		<title>自动切换图片、数字按钮组及数字按钮click事件</title>
		<style>
			#pictureBox {
				position: relative;
				border: 1px solid black;
			}/*图片框*/
			#pictureBox img {
				border-width: 0px;
				overflow: hidden;
			}/*图片*/
			#buttonGroup {
				position: absolute;
				left: 0px;
				bottom: 0px;
				height: 16px;
				background-color: sliver;
				filter: alpha(opacity=60); /*数字按钮组*/
				-moz-opacity:0.6;
				 opacity:0.6;
			}
			#buttonGroup div {
				float: right;
			}
			#buttonGroup span {
				float: left;
				padding: 0 8px;
				border-left: 1px solid #ccc;
				background-color: gray;
				line-height: 16px;
				font-size: 16px;
				color: white;
				cursor: pointer;
			}
		</style>
	</head>
	<body onload="init()">
		<div id="pictureBox"><!---图片框,仅包含图片,数字按钮组----->
			<a href="" id="linkURL"><img src="" id="pic" alt=""/></a><!-----图片及超链接---->
		<div id="buttonGroup"><!---数字按钮组---->
			<div>
				<span id="Button0" onclick="changePictureByClick(0)">1</span>
				<span id="Button1" onclick="changePictureByClick(1)">2</span>
				<span id="Button2" onclick="changePictureByClick(2)">3</span>
				<span id="Button3" onclick="changePictureByClick(3)">4</span>
			</div>
		</div>	
		</div>
		<script>
			var pictureBoxWidth=500;//图片框宽度,取其中最小值
			var pictureBoxHeight=341;//图片框高度,取其中最小值
			var imgSrc=new Array("img/1.png","img/2.png","img/3.png","img/4.png");//图片
			var aHref=["http://www.sina.com","http://www.sina.com","http://www.sina.com","http://www.sina.com"];//图片超链接
			var interval=2000;//每张图片延时时间(毫秒)
			var pictureIndex=0;//将要显示的图片编号
			var imgElement=document.getElementById("pic");//获取插入图片的img元素
			var aElement=document.getElementById("linkURL");//获取插入图片上的a元素
			
											//load事件处理函数
			function init() {		//设置图片框的宽度和高度
				var divElement=document.getElementById("pictureBox");
				divElement.style.width=pictureBoxWidth+"px";
				divElement.style.height=pictureBoxHeight+"px";//设置图片的宽度和高度
				imgElement.style.width=pictureBoxWidth+"px";
				imgElement.style.height=pictureBoxHeight+"px";
				
				//设置数字按钮组的宽度
				divElement=document.getElementById("buttonGroup");
				divElement.style.width=pictureBoxWidth+"px";
			}
			
			function autoChangePicture(pictureID) {
				imgElement.src=imgSrc[pictureID];//更换图片
				aElement.href=aHref[pictureID];//更换图片超链接
				var previousPictureIndex=(pictureIndex==0)?3:(pictureIndex-1);
				document.getElementById("Button"+previousPictureIndex).style.backgroundColor="gray";
				document.getElementById("Button"+pictureID).style.backgroundColor="red";
				document.getElementById("Button"+pictureID).innerHTML=(pictureID+1).toString();
				pictureIndex=(pictureID+1)%4;
			}
			autoChangePicture(pictureIndex);//每隔interval毫秒调用一次autoChangePicture函数
			var timerID=setInterval("autoChangePicture(pictureIndex)",interval);
			//鼠标滑过图片时,暂停图片切换
			imgElement.onmouseover=function() {
				clearInterval(timerID);
			}
			//鼠标离开图片时,继续图片自动切换
			imgElement.onmouseout=function() {
				timerID=setInterval("autoChangePicture(pictureIndex)",interval);
			}
			
			function changePictureByClick(newPictureIndex) {
				clearInterval(timerID);
				autoChangePicture(newPictureIndex);
				timerID=setInterval("autoChangePicture(PictureIndex)",interval);
			}
		</script>
	</body>
</html>

读者可自我体会,原理暂不概述。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高中不复,大学纷飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值