实现简单轮播器

HTML:

<DOCTTYPE!>
<html>
<head>
<meta charset='utf-8'>
<script src="index.js" type="text/javaScript"></script>
<link href="index.css" type="text/css" rel="stylesheet">
</head>
<body>
<div id='box'>
<img src='images/0.jpg' alt='促销' id='image0'>
<img src='images/1.jpg' alt='促销' id='image1'>
<img src='images/2.jpg' alt='促销' id='image2'>
<img src='images/3.jpg' alt='促销' id='image3'>
</div>
<div id='click'>

<img src = 'images/4.jpg' alt='点击' id='click2'>
</div>
</body>
</html>

CSS:

*{
margin: 0;
padding: 0;
}
#image0,#image1,#image2,#image3{
position: absolute;
top: 0;
left: 0;
}
#click{
position: absolute;
top: 240px;
left: 490px;
}
#image0{
opacity: 1;
}
#image1{
opacity: 0;
}
#image2{
opacity: 0;
}
#image3{
opacity: 0;
}
#click2{
cursor: pointer;
}

JavaScript:

window.onload = function(){
shoudong(4);
zidong(4);
}


//手动轮播器 imgNum:轮播器中图片的数量
function shoudong(imgNum){
var click1 = document.getElementById('click1');
var box = document.getElementById('box');
var child = box.children;
var num = 1;
click2.onclick = function(){
if(num == imgNum){
num = 0;
}
if(num == 0){
child[imgNum-1].style.opacity = 0;
}else{
child[num-1].style.opacity = 0;
}
child[num].style.opacity = 1;
num++;
}
}

//自动轮播器 imgNum:轮播器中图片的数量
var timer = null;
function zidong(imgNum){
var click1 = document.getElementById('click1');
var box = document.getElementById('box');
var child = box.children;
var num = 1;
timer = setInterval(function(){
if(num == imgNum){
num = 0;
}
if(num == 0){
child[imgNum-1].style.opacity = 0;
}else{
child[num-1].style.opacity = 0;
}
child[num].style.opacity = 1;
num++;
},1000);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值