原生js轮播图

2018-06-24 01:02:58来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

//用原生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>

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Media 媒体查询

下一篇:繁星CSS3之旅-CSS基本样式-CSS文本字体