原生js轮播图

//用原生js实现了一个简单的轮播图效果

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#banner{
position: relative;
}
#list{
width: 750px;
height: 500px;
margin: 100px auto;
}
#list img{
display: block;
}
#list1{
position: absolute;
bottom: 10px;
left: 50%;
margin-left: -75px;
}
#list1 li{
list-style: none;
width: 20px;
height: 20px;
border-radius: 20px;
background: #000;
opacity: .4;
filter: alpha(opacity = 40) ;
float: left;
margin-left:10px ;
cursor: pointer;
}
#banner a{
display: block;
width: 30px;
height: 30px;
background: #000;
opacity: .4;
position: absolute;
top: 50%;
margin-top: -15px;
text-decoration: none;
color: #fff;
font-size: 20px;
font-weight: 600;
line-height: 30px;
text-align: center;
}
#left{
left: 21%;
}
#right{
right: 21%;
}
#list1 .li{
background: #f40;
}
#box{
width: 750px;
height: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="banner">
<div id="box">
<div id="list">
<img id="pic" src="img/1.jpg" />
</div>
<a href="javascript:;" id="left"><</a>
<a href="javascript:;" id="right">></a>
</div>
<ul id="list1">
<li class="li"></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<script type="text/javascript">
var oList = document.getElementById("list");
var oList1 = document.getElementById("list1");
var oLi = document.getElementsByTagName('li');
var oPic = document.getElementById("pic");
var Left = document.getElementById("left");
var Right = document.getElementById("right");
var box = document.getElementById("box");
num = 1;
var timer = setInterval(function(){
num++;
if(num>5){
num=1;
}
fn();
},2000)
oList.onmouseout = function(){
timer = setInterval(function(){
num++;
if(num>5){
num=1;
}
fn();
},2000)
}
box.onmouseover = function(){
clearInterval(timer);
}
Right.onclick = function(){
num++;
if(num>5){
num =1;
}
fn();
}
Left.onclick = function(){
num--;
if(num<1){
num = 5;
}
fn();
}
for (var i=0;i<oLi.length;i++) {
oLi[i].index = i;
oLi[i].onclick = function(){
num = this.index+1;
fn();
}
}
function fn(){
oPic.src = "img/"+num+".jpg";
for (var i=0;i<oLi.length;i++) {
oLi[i].className = '';
if(num == i+1){
oLi[i].style.background = '#f40';
}else{
oLi[i].style.background = '';
}
}
}
</script>
</body>
</html>

转载于:https://www.cnblogs.com/zzgyq/p/banner.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值