html,css,js实现轮播图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        ul{
            list-style: none;
        }
        .wrap{
            width: 800px;
            height: 400px;
            position: relative;
        }
        .list{
            width: 800px;
            height: 400px;
            position: relative;
        }
        .list .item{
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            transition:all .5s;
        }
        .list li:nth-child(1){
            background-color: yellow;
        }
        .list li:nth-child(2){
            background-color: green;
        }
        .list li:nth-child(3){
            background-color: pink;
        }
        .list li:nth-child(4){
            background-color: black;
        }
        .btn{
            width: 50px;
            height: 100px;
            position: absolute;
            top: 150px;
            z-index: 10000;
        }
        #goPre{
            left: 0px;
        }
        #goNext{
            right: 0px;
        }
        .item.active{
            z-index: 100;
            opacity: 1;
        } 
        .pointList{
            position: absolute;
            right: 20px;
            bottom: 20px;
            z-index: 1000;
        }
        .pointList .point{
            width: 5px;
            height: 5px;
            margin-right: 5px;
            border-radius: 100%;
            border: 2px solid rgba(255, 255,255, 0.6);
            background-color: rgba(0, 0,0, 0.4);
            float: left;
        }
        .point.active{
            background-color:rgba(255, 255,255, 0.8);
        }
    </style>
</head>
<body>
    <div class="wrap">
        <ul class="list">
            <li class="item active"></li>
            <li class="item"></li>
            <li class="item"></li>
            <li class="item"></li>
        </ul>
            <button type="button" class="btn" id="goPre">&lt</button>
            <button type="button" class="btn" id="goNext">&gt</button>
            <ul class="pointList">
                <li class="point" data-index="0"></li>
                <li class="point" data-index="1"></li>
                <li class="point" data-index="2"></li>
                <li class="point" data-index="3"></li>
            </ul>
    </div>
    <script>
        var items=document.getElementsByClassName('item');/*获取图*/
        var goPreBtn=document.getElementById('goPre');/*获取按钮*/
        var goNextBtn=document.getElementById('goNext');/*获取按钮*/
        var points=document.getElementsByClassName('point');/*获取点*/
        var time=0;
        var index=0;   /*index表示显示第几张,是连接点,图,按钮的关键*/
        /*清除上一次选中效果*/
        function clearActive(){
            for(var i=0;i<items.length;i++){
                items[i].className='item';
                points[i].className='point';
            }
        }
        /*实现显示去到index页面的效果*/
        function goIndex(){
            clearActive();
            items[index].className='item active';
            points[index].className='point active';
        }
        /*向后*/
        function goNext(){
            if(index<3){
                index++;
            }
            else{
                index=0;
            }
            goIndex();
        }
        /*向前*/
        function goPre(){
            if(index>0){
                index--;
            }
            else{
                index=3
            }
            goIndex()
        }
        /*向后按钮监听*/
        goNextBtn.addEventListener('click',function(){
            goNext();
        })
        /*向前按钮监听*/
        goPreBtn.addEventListener('click',function(){
            goPre();
        })
        /*给点设置监听实现跳转*/
        for(var i=0;i<points.length;i++){
            points[i].addEventListener('click',function(){
                var pointIndex=this.getAttribute('data-index');
               index=pointIndex;
               goIndex();
               time=0;
            })
        }
        /*设置定时器*/
        setInterval(function(){
            time++;
            if(time==20){
            goNext();
        /*每次点击后将time置0确保等待10s*/
            time=0;
        }
        },100)
    </script>
</body>
</html>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值