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;
}

 

發佈了101 篇原創文章 · 獲贊 65 · 訪問量 4352
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章