js 实现轮播图 代码

<!--

仅作为作者个人笔记,有幸搜到此篇文章的 小白 希望对你有用

-->

<!--样式部分-->

*{
margin: 0px;
padding: 0px;
}
body{
color: #FF4500;
font-family: "微软雅黑";
}
.main{
width: 1200px;
height: 460px;
margin: 30px auto;/*控制距离上边30像素   居中显示*/
overflow: hidden;/*超出截取*/
position: relative;
border: 1px solid #000;
}
.banner{
width: 1200px;
height: 460px;
overflow: hidden;/*超出截取*/
position: relative;
border: 1px solid #ffo;
}
.banner-list{
width: 1200px;
height: 460px;
position: absolute;
background-repeat:no-repeat;
display: none;
border: 1px solid #0f0;
}
.slide1{
background-image:url(img/bg.jpg) ;/*这个属性必须写全  不然上个属性no-repeat无效*/
}
.slide2{
background-image:url(img/bg2.jpg) ;
}
.slide3{
background-image: url(img/bg3.jpg);
}
.show{
display: block;
}
.button{
position: absolute;
width: 40px;
height: 80px;
top:50%;
margin-top:-40px;
background:url(img/left.png)no-repeat center center;/*后面两个center设置水平、垂直都居中*/
left: 244px;
}
.button:hover{/*鼠标触碰箭头滑动效果*/
background-color: #e5e5e5;
opacity: 0.8;
filter:alpha(opactiy=80);/*为兼容各浏览器*/
}
.up{
left: auto;
right: 0;
}
.up{
transform: rotate(180deg);/*css3中转换图片方向代码*/
}
.point{
position: absolute;
right: 50px;
bottom: 24px;
text-align: right;
}
.point span{
display: inline-block;
width:12px;
height: 12px;
line-height: 12px;
border-radius: 50%;
background-color: rgba(7,17,27,0.4);
box-shadow: 0 0 0 2px rgba(255,255,255,0.8) inset;
margin-left: 8px;
cursor: pointer;
}
.point span.action{
box-shadow: 0 0 0 2px rgba(7,17,27,0.4) inset;
background: #fff;

}

<!--body部分-->

<body>
<!--
          首先设定整个轮播图的 大框main
        -->
<div class="main" id="main">
<!--设置轮播图的框架banner-->
<div class="banner" id="banner">
<!--然后设置轮播图内的图片-->
<a href="">
<div class="banner-list slide1 show"></div>
</a>
<a href=""> 
<div class="banner-list slide2"></div>
</a>
<a href="">
<div class="banner-list slide3"></div>
</a>
</div>
<!--轮播图中的两个箭头-->
<a href="javascript:void(0)" class="button up" id="prve"></a>
<a href="javascript:void(0)" class="button down" id="brve"></a>
<!--轮播图中的圆点-->
<div class="point" id="dots">
<span class="action"></span>
<span></span>
<span></span>
</div>
</div>

</body>

<!--js 部分-->

<script type="text/javascript">
/*首先封装一个代替getElementById()的方法,,,省着每次都要手写document.getElementById()这么长的代码*/
function byid(id){
/*首先条件:检测Id类型和值都要等于字符串,如果是则执行代码1:document.getElementById(id),否则执行代码2:id*/
return typeof(id) === "string"?document.getElementById(id):id;//三元操作符
/*return 弹出这个条件*/
}

var index = 0,//定义一个全局变量,记录索引
   timer = null,//定时器
   pics = byid("banner").getElementsByTagName("div"),//取id为banner下的div 得到数组
   dots = byid("dots").getElementsByTagName("span"),
   prve = byid("prve"),
   brve = byid("brve"),
   len = pics.length;//定义len得到数组pics的长度
/*接下来将所有操作定义在函数slideImg里*/
function slideImg(){
var main = byid("main");//获取id为main的dom元素
//鼠标滑过时清除定时器,离开继续
main.onmouseover = function(){//鼠标划过时触发
//如果timer为真时 清除定时器;
if(timer) clearInterval(timer);//clearInterval:清除定时器;
}
main.onmouseout = function(){//鼠标离开时触发
timer = setInterval(function(){//每隔3秒调用一次里面的脚本
index++;//每隔3秒索引+1
if(index >= len){//判断如果索引值大于等于数组长度,就强制让它返回索引为0的图片
index = 0;
}
//切换图片
charImg();
},3000);//间歇调用
}
//调用onmouseout方法,自动在main上触发鼠标离开的事件。
main.onmouseout();//进行自动轮播,无需触碰

//遍历所有点击,且绑定点击事件,点击圆点切换图片
for(var d = 0; d<len ; d++){
/*给所有span添加一个id的属性,值为d,作为当前span的索引*/
dots[d].id = d;
dots[d].onclick = function(){//function()函数能够改变作用域
//改变Index为当前span的Id值
index = this.id;
this.className = "active";//this 指向对象 相当于span
//调用charImg,实现图片的切换
charImg();
}
}
//上一张按钮
brve.οnclick=function(){
index++;
if(index >= len) index=0;
charImg();
}
//下一张
prve.οnclick=function(){
index--;
if(index < 0) index = len-1;
charImg();
}
}

//切换图片
function charImg(){
//遍历banner下的所有的div,将其隐藏
for(var i = 0;i<len;i++){//遍历所有banner下的div以及dots下的span,并把每个div都隐藏,并将span类清除。
pics[i].style.display = "none";
dots[i].className = "";
}
//根据index索引找到当前div和span,将其显示出来并设为当前。
pics[index].style.display='block'; 
dots[index].className = "action";
};
slideImg();//调用slideImg()方法
</script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值