用js简单实现图片轮播

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    *{
        padding: 0;
        margin: 0;
    }
    .main{
        height: 300px;
        width: 560px;
        /*border: 1px solid;*/
        margin: 100px auto;
        position: relative;
    }
    .banner-slide{
        height: 300px;
        width: 560px;
        position: absolute;
        overflow: hidden;
        display: none;
    }
    .slide1{
        /*position: absolute;*/
        background: url("img/B1.jpg");
    }
    .slide2{

        background: url("img/B2.jpg");
    }
    .slide3{
        background: url("img/B3.jpg");
    }
    .slide4{
        background: url("img/B4.jpg");
    }
    .slide-active{
        display: block;
    }
    
    .button{
        width: 30px;
        height: 50px;
        position: absolute;
        font-size: 35px;
        text-decoration: none;
        color: red;
    }
    .prev{
        left: 395px;
        top: 210px;
    }

    .next{
        right: 395px;
        top: 210px;
    }
    .button:hover{
        background-color: #333333;
        opacity: 0.5;
    }

    .dots{
    position: absolute;
    right: 405px;
    bottom: -22px;
    text-align: right;
}
.dots span{
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius:50%;
    line-height: 12px;
    background: rgba(7,17,27,0.4);
    /*阴影:X Y Z 模糊度 颜色 */
    box-shadow: 0 0 0 2px rgba(255,255,255,0.8) inset;
    margin-left: 10px;
    cursor:pointer;
}
.dots span.active{
    box-shadow: 0 0 0 2px rgba(7,17,27,0.4) inset;
    background: #ffffff;
}
    </style>
</head>
<body>
    <div class="main" id="main">
        <div class="banner" id="banner">
            <a href="">
            <div class="banner-slide slide1 slide-active"></div>
        </a>
        <a href="">
            <div class="banner-slide slide2"></div>
        </a>
        <a href="">
            <div class="banner-slide slide3"></div>
        </a>
        <a href="">
            <div class="banner-slide slide4"></div>
        </a>
        </div>
        
    </div>
    <!-- 上一张,下一张 -->
    <a href="javascript:void(0)" class="button prev" id="prev">&lt;</a>
    <a href="javascript:void(0)" class="button next" id="next">&gt;</a>
    <!-- 圆点点击事件 -->
    <div class="dots" id="dots">
        <span class="active"></span>
        <span></span>
        <span></span>
        <span></span>
    </div>
<script>
    // 封装一个代替getElementByID()的方法
    function byId(id){
        return typeof(id)==="string"?document.getElementById(id):id;
        
    }
// console.log(document.getElementById("main"));
    var index=0;
    var timer=null;
    var pics=byId("banner").getElementsByTagName("div");
    var len=pics.length;
    var dots=byId("dots").getElementsByTagName("span");
    var prev=byId("prev");
    var next=byId("next");

    function slideImg(){
        var main=byId("main");
        // 滑过清除定时器,离开继续
       main.οnmοuseοver=function(){
            if(timer) clearInterval(timer);
       }
       main.οnmοuseοut=function(){
           timer=setInterval(function(){
               index++;
               if(index>=len){
                   index=0;
               }
               changImg();
               console.log(index);
           },2000);
       }
      
      // 自动在main上触发事件
       main.onmouseout();

    // 圆点点击事件,实现轮播
    for(var j=0;j<len;j++){
        // 给所有的span添加一个id的属性,值为j,作为当前的索引
        dots[j].id=j;
        dots[j].οnclick=function(){
            // 改变index为当前span的id值
            index=this.id;
            // 调用函数
            changImg();
            // alert("hello")
        }
    }

     // // 上一张、下一张事件
    next.οnclick=function(){
        index++;
        if(index>=len) index=0;
        // cosole.log(index)
        changImg();
    }

    prev.οnclick=function(){
        index--;
        if(index<0) index=len-1;
        changImg();
    }
    }

    // 切换图片
    function changImg(){
        for(var i=0;i<len;i++){
            pics[i].style.display="none";
            dots[i].className="";
        }
        pics[index].style.display="block";
        dots[index].className="active";
    }

    slideImg();
</script>
</body>
</html>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值