使用原生js实现轮播图

HTML部分

<div id="box">
    <a id="left" href="javascript:;"> ← </a>
    <a id="right" href="javascript:;"> → </a>
    <div id="fonttop"></div>
    <img id="image" />
    <div id="fontbottom"></div>
</div>

css部分

                        #box{
				width:1280px;
				height: 544px;
				border: 2px solid red;
				overflow: hidden;
				margin: 100px auto;
			}
			#left{
				width: 50px;
				height: 50px;
				background: rgba(0,0,0,0.5);
				font-family: "微软雅黑";
				font-size: 35px;
				text-align: center;
				text-decoration: none;
				color: white;
				position: absolute;
				top: 350px;
				left: 160px;
			}
			#right{
				width: 50px;
				height: 50px;
				background: rgba(0,0,0,0.5);
				font-family: "微软雅黑";
				font-size: 35px;
				text-align: center;
				text-decoration: none;
				color: white;
				position: absolute;
				top: 350px;
				left: 1300px;
			}
			#fonttop{
				width: 1280px;
				height: 50px;
				background: rgba(40,20,0,0.5);
				position: absolute;
				color: white;
				text-align: center;
			}
			#fontbottom{
				width: 1280px;
				height: 50px;
				background: rgba(40,20,0,0.5);
				position: absolute;
				top: 596px;
				color: white;
				text-align: center;
			}

js部分

                        var lefts=document.getElementById("left");
			var rights=document.getElementById("right");
			var fonttops=document.getElementById("fonttop");
			var images=document.getElementById("image");
			var fontbottoms=document.getElementById("fontbottom");
			var num=0;
			
			var img=["../img/1.jpg","../img/2.jpg","../img/3.jpg","../img/4.jpg","../img/5.jpg"];
			var font=["文字一","文字二","文字三","文字四","文字五"];
			fonttops.innerHTML=num+1 +"/"+img.length;
			images.src=img[num];
			fontbottoms.innerHTML=font[num];
			var time=setInterval(times,1000);
			function tsb(){
				fonttops.innerHTML=num+1 +"/"+img.length;
				images.src=img[num];
				fontbottoms.innerHTML=font[num];
			}
			function times(){
				num++;
			 	if(num==img.length){
			 		num=0;
			 	}
			 	tsb();
			}
				
			rights.οnclick=function(){
				times();
			 }
			lefts.οnclick=function(){
				num--;
				if(num<0){
					num=img.length-1;
				}
				tsb();
			}
			
			images.οnmοusemοve=function(){
				clearInterval(time);
			}
			images.οnmοuseοut=function(){
				time=setInterval(times,1000);
			}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值