天天看點

呼吸輪播特效

呼吸輪播特效

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>呼吸輪播特效</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #box {
            width: 202px;
            height: 252px;
            border: 1px solid #000;
            margin: 100px auto;
            position: relative;
            /* overflow: hidden; */
        }

        ul {
            list-style-type: none;
            position: relative;
            /* width: 1000px; */
        }

        ul li {
            /* 圖檔都放在一起,設定透明度為0 */
            position: absolute;
            top: 0;
            left: 0;
            opacity: 0;
            transition: all 1s ease 0s;
        }

        ul li:first-child {
            /* 顯示第一張圖檔 */
            opacity: 1;
        }

        ul li img {
            width: 200px;
            height: 250px;
        }

        #leftbtn {
            width: 20px;
            height: 20px;
            background-color: rgb(127, 171, 230, .5);
            position: absolute;
            left: 10px;
            top: 50%;
            margin-top: -10px;
            border-radius: 50%;
        }

        #rightbtn {
            width: 20px;
            height: 20px;
            background-color: rgb(127, 171, 230, .5);
            position: absolute;
            right: 10px;
            top: 50%;
            margin-top: -10px;
            border-radius: 50%;
        }
    </style>
</head>

<body>
    <!-- 呼吸輪播圖特效,動畫和js結合 老圖淡出,新圖淡入 -->
    <div id="box">
        <ul id='list'>
            <li><img src="images/f1.jpg" alt=""></li>
            <li><img src="images/f2.jpg" alt=""></li>
            <li><img src="images/f3.jpg" alt=""></li>
        </ul>
        <a href="javascript:;" id="leftbtn"></a>
        <a href="javascript:;" id="rightbtn"></a>
    </div>
</body>
<script>
    var list = document.getElementById('list')
    var leftbtn = document.getElementById('leftbtn')
    var rightbtn = document.getElementById('rightbtn')
    var lis = list.getElementsByTagName('li')

    // 數到第幾張圖了
    var ind = 0

    var lock = true

    // 點選右按鈕,切換圖檔
    rightbtn.onclick = function () {

        if (!lock) return

        // 老圖淡出
        lis[ind].style.opacity = 0
        // 淡出完後換圖
        ind++
        if (ind > 2) {
            ind = 0
        }
        // 新圖淡入
        lis[ind].style.opacity = 1

        lock = false
        setTimeout(function () {
            lock = true
        }, 1000)
    }
    // 點選左按鈕,切換圖檔
    leftbtn.onclick = function () {

        if (!lock) return

        // 老圖淡出
        lis[ind].style.opacity = 0
        // 淡出完後換圖
        ind--
        if (ind < 0) {
            ind = 2
        }
        // 新圖淡入
        lis[ind].style.opacity = 1

        lock = false
        setTimeout(function () {
            lock = true
        }, 1000)
    }
</script>

</html>
           

繼續閱讀