js实现简单轮播图效果

1.实现效果

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>简易轮播图</title>
		<style type="text/css">
			*{
				margin: 0px;
				padding: 0px;
			}
			#one{
				width: 600px;
				height: 540px;
				border: 1px solid red;
				margin: 0 auto;
				position: relative;
			}
			#one>img{
				width:600px;
				height:540px;
				position: absolute;
				opacity: 0;
				transition: all  0.3s linear;
			}
			#one>.active{
				opacity: 1;
			}
			#dian{
				width: 500px;
				height: 50px;
				position: absolute;
				left: 10%;
				bottom: 40px;
			}
			.dian{
				width: 50px;
				height: 50px;
				border: 1px solid bisque;
				float: left;
				margin-left: 20px;
				border-radius: 50%;
			}
			#dian>.dactive{
				background: deepskyblue;
			}
		</style>
	</head>
	<body>
		<div id="one">
			<img src="../19放大镜/04.放大镜操作/104764b93ce519ea580ea2114cd0fe8b.jpg"/>
			<img src="../上下换图/img/w.jpg"/>
			<img src="../上下换图/img/wi.jpg"/>
			<img src="../放大镜重要/322059.jpg"/>
			<img src="../19放大镜/5.jpg"/>
			<div id="dian">、
				<div class="dian dactive"></div>
				<div class="dian"></div>
				<div class="dian"></div>
				<div class="dian"></div>
				<div class="dian"></div>
			</div>
		</div>
	</body>
	<script type="text/javascript">
		//定位元素
		var imgs=Array.from(document.getElementsByTagName('img'));
		var dians=Array.from(document.getElementsByClassName('dian'));
		//定义一个出始值的下标
		
		var index=0;//全局变量
		//设置定时器用于,每隔一秒中去更换图片与点
		//设置一个函数,用设值定时器的开始
		function go(){
			
			mytime=setInterval(function(){
				//让现在显示的图片与点去隐藏
				imgs[index].removeAttribute('class');
			dians[index].setAttribute('class','dian');
			//让index自增
			index++;
			 
			//加入判断
			if(index>imgs.length-1){
				index=0;
			}
			dians[index].setAttribute('class','dian dactive');
			imgs[index].setAttribute('class','active');
		},1000)
			}
			go();
			
			one.onmouseover=function(){
				clearInterval(mytime);
				
			}
			one.onmouseout=function(){
				go();
			}
		
		dians.forEach(function(v,k){
			v.onclick=function(){
				dians.forEach(function(val,key){
					val.setAttribute('class','dian');
					imgs[key].removeAttribute('class')
				})
				index=k;
				this.setAttribute('class','dian dactive');
				imgs[k].setAttribute('class','active')
			}
		})
	</script>
</html>

 

 

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值