好例题------轮播图

<style>
        #box{width: 590px;height: 470px;position: relative;margin-left: 400px}
        #con div{display: none}
        #con div.one{display: inline-block;}
        #head{width: 100%;position: absolute;top: 50%;margin-top: -16px}
        #head .pic{background: rgba(0, 0,0, 0.3)}
        #head .pic1{float: left;}
        #head .pic2{float: right;}
        #pc{position: absolute;bottom: 10px;left: 50%;transform: translateX(-50%);}
        #pc li{list-style: none;width: 10px;height: 10px;border-radius: 50%;border: 1px solid #f4f4f4;float: left;margin-left: 10px}
        #pc .two{background: #fff;}
</style>
<body>
    <div id="box">
        <!-- 轮播图 -->
        <div id="con">
            <div class="one"><img src="img/02.jpg"></div>
            <div><img src="img/03.jpg"></div>
            <div><img src="img/04.jpg"></div>
            <div><img src="img/05.jpg"></div>
        </div>
        <!-- 箭头 -->
        <div id="head">
            <img src="img/l1.png" class="pic pic1">
            <img src="img/l2.png" class="pic pic2">
        </div>
        <!-- 小圆点插件 -->
        <ul id="pc">
            <li class="two"></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
    <script>
        //获取轮播图片
        var con=document.getElementById('con').getElementsByTagName('div');
        //获取箭头图片
        var head=document.getElementById('head').getElementsByTagName('img');
        //获取最大盒子轮播区域
        var box=document.getElementById('box');
        //获取小圆点插件
        var pc=document.getElementById('pc').getElementsByTagName('li');
        var index=0;//图片索引
        var t;//定时器编号
        //每隔2秒执行一次cont函数
        t=setInterval(cont,2000);
        //小圆点插件点击事件
        for(var j=0;j<pc.length;j++){
            pc[j]._index=j;
            pc[j].onclick=function(){
                //清除当前样式
                pc[index].className='';
                con[index].className='';
                //修改下一个的样式
                con[this._index].className='one';
                pc[this._index].className='two';
                index=this._index;
            }
        }

        //鼠标放在图片上图片不再轮播
        box.onmouseenter=function(){
            this.style.cursor='pointer';
            clearInterval(t);
        };
        //鼠标离开图片上图片继续轮播
        box.onmouseleave=function(){
            this.style.cursor='default';
            setInterval(cont,2000);
        }
        //箭头触碰事件
        for(var i=0;i<head.length;i++){
            head[i].onmouseenter=function(){
                this.style.background='rgba(0,0,0,0.6)'
            };
            head[i].onmouseleave=function(){
                this.style.background='rgba(0,0,0,0.3)'
            }
        }
        //右箭头点击事件
        head[1].onclick=function(){
            cont();
        }
        //左箭头点击事件
        head[0].onclick=function(){
            conts();
        }
        
        //下一张变换
        function cont(){
            //清除当前样式
            pc[index].className='';
            con[index].className='';
            index++;
            if(index==con.length){
                index=0;
            }
            con[index].className='one';
            pc[index].className='two';
        }
        //上一张变换
        function conts(){
            //清除当前样式
            pc[index].className='';
            con[index].className='';
            index--;
            if(index==-1){
                index=con.length-1;
            }
            con[index].className='one';
            pc[index].className='two';
        }
    </script>
</body>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值