js常用项目——轮播图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css.css">
</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>
            <li class="item"></li>
        </ul>

        <ul class="pointlist">
            <li class="point active"></li>
            <li class="point"></li>
            <li class="point"></li>
            <li class="point"></li>
            <li class="point"></li>
        </ul>
        <button type="button" class="btn" id="goPre"><</button>
        <button type="button" class="btn" id="goNext">></button>

    </div>
    <script>
        var items = document.getElementsByClassName('item');
        var goPrebtn = document.getElementById('goPre');
        var goNextbtn = document.getElementById('goNext');
        var points = document.getElementsByClassName('point');

        var index = 0;

        var goIndex = function()
        {
            for(var i = 0; i < items.length; i++)
            {
                items[i].className = 'item';
            }
            for(var i = 0; i < points.length; i++)
            {
                points[i].className = 'point';
            }
            items[index].className = 'item active';
            points[index].className = 'point active';
        }

        var goNext = function()
        {
            if(index < 4)
            {
                index++;
            }
            else index = 0;
            goIndex();
        }

        var goPre = function()
        {
            if(index == 0)
            {
                index = 4;
            }
            else index--;
            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;
               })
           }

           var time = 0;
           setInterval(function()
           {
               time++;
               if(time == 20)
               {
                goNext();
                time = 0;
               }
           }, 100)
    </script>
</body>
</html>

css

.wrap
{
    width: 800px;
    height: 400px;
    position: relative;
}
.list
{
    width: 800px;
    height: 400px;
    padding-left: 0px;
}
.item
{
    width: 100%;
    height: 100%;
    color: white;
    font-size: 20px;
    position: absolute;
    opacity: 0;
    transition: all .10s;
}
.item:nth-child(1)
{
    background-color: teal;
}
.item:nth-child(2)
{
    background-color: rebeccapurple;
}
.item:nth-child(3)
{
    background-color: hotpink;
}
.item:nth-child(4)
{
    background-color: coral;
}
.item:nth-child(5)
{
    background-color: green;
}
.item.active
{
    z-index: 10;
    opacity: 1;
}
.btn
{
    width: 50px;
    height: 100px;
    position: absolute;
    top: 150px;
    z-index: 100;
}
#goPre
{
    left: 0px;
}
#goNext
{
    right: 0px;
}
.pointlist
{
    padding-left: 0px;
    list-style: none;
    position: absolute;
    right: 20px;
    bottom: 10px;
    z-index: 100;
}
.point
{
    width: 8px;
    height: 8px;
    background-color: rgba(0, 0, 0, 4);
    border-radius: 100%;
    float: left;
    margin-left: 12px;
    border-style: solid;
    border-width: 2px;
    border-color: rgba(225, 225, 225, 0.6);
}
.point.active
{
    background-color: rgba(225,225,225, 0.2);
    z-index: 100;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值