css3 transition 實現炫酷鋼琴鍵 炫酷

<style>
    * {
        margin: 0;
        padding: 0;
    }

    ul {
        list-style: none;
    }

    .wrapper {
        width: 100vw;
        height: 100vh;
        background: #000;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .wrapper ul {
        width: 80%;
        height: 80%;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .wrapper ul li {
        width: 16%;
        height: 100%;
        border-radius: 20px;
        background: #333;
        overflow: hidden;
        /* 這是第二次運動 ,關閉時候調用*/
        transition: width .5s linear, height .5s linear .5s;
    }

    .wrapper ul li .inner {
        width: 100%;
        height: 100%;
        position: relative;
        transition: all .5s linear;
    }

    .wrapper .init li .inner {
        transform: translateY(100%);
    }

    .wrapper ul li .inner .bg {
        width: 100%;
        height: 100%;
        background-position: center;
        background-size: cover;
        cursor: pointer;
        opacity: 0.5;
    }

    .wrapper ul li .inner h2 {
        font-size: 16px;
        color: #fff;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
    }

    .wrapper ul li:nth-of-type(1) .inner .bg {
        background-image: url("img/1.jpg");
    }

    .wrapper ul li:nth-of-type(2) .inner .bg {
        background-image: url("img/2.jpg");
    }

    .wrapper ul li:nth-of-type(3) .inner .bg {
        background-image: url("img/3.jpg");
    }

    .wrapper ul li:nth-of-type(4) .inner .bg {
        background-image: url("img/4.jpg");
    }

    .wrapper ul li:nth-of-type(5) .inner .bg {
        background-image: url("img/5.jpg");
    }

    .wrapper ul li:nth-of-type(6) .inner .bg {
        background-image: url("img/6.jpg");
    }

    .wrapper ul li:hover .inner .bg {
        opacity: 1;
        transition: all .2s linear;
    }

    .wrapper ul li:hover h2 {
        font-size: 24px;
        transition: all .2s linear;
    }

    .wrapper ul li:nth-of-type(1) .inner {
        transition-delay: 0.1s;
    }

    .wrapper ul li:nth-of-type(2) .inner {
        transition-delay: 0.2s;
    }

    .wrapper ul li:nth-of-type(3) .inner {
        transition-delay: 0.3s;
    }

    .wrapper ul li:nth-of-type(4) .inner {
        transition-delay: 0.4s;
    }

    .wrapper ul li:nth-of-type(5) .inner {
        transition-delay: 0.5s;
    }

    .wrapper ul li:nth-of-type(6) .inner {
        transition-delay: 0.6s;
    }

    .wrapper ul .inner .direction {
        width: 100%;
        height: 30px;
        position: absolute;
        top: 50px;
        padding: 0 30px;
        display: flex;
        justify-content: space-between;
        box-sizing: border-box;
        align-items: center;
        opacity: 0;
    }

    #activeWrap li .inner h2 {
        opacity: 0;
        transition: all 0.2s linear;
    }

    #activeWrap li:not(.activeLi) {
        width: 0;
        height: 0;
        transition: all .5s linear;
    }

    #activeWrap li.activeLi {
        width: 100%;
    }

    #activeWrap li.activeLi .inner .bg {
        opacity: 1;
    }

    #activeWrap li {
        transition: height .5s linear, width .5s linear .5s;
    }

    .activeLi .inner .direction .title {
        color: #ffff;
        font-size: 24px;
    }

    .activeLi .inner .direction .close {
        width: 30px;
        height: 30px;
        cursor: pointer;
        position: relative;
    }

    .activeLi .inner .direction .close::before,
    .activeLi .inner .direction .close::after {
        content: '';
        width: 30px;
        height: 4px;
        position: absolute;
        top: 50%;
        margin-top: -2px;
        background: #fff;

    }

    .activeLi .inner .direction .close::before {
        transform: rotate(45deg);
    }

    .activeLi .inner .direction .close::after {
        transform: rotate(-45deg);
    }

    #activeWrap li .inner .direction {
        opacity: 1;
        transition: opacity .5s linear;
        transition-delay: 1s;
    }

    #activeWrap li .inner .direction .close {
        transform: rotate(360deg);
        transition: all 0.5s linear 1s;
    }
</style>

結構

<div class="wrapper">
    <ul class="init">
        <li>
            <div class="inner">
                <div class="bg"></div>
                <h2>image1</h2>
                <div class="direction">
                    <div class="title">bird1</div>
                    <div class="close"></div>
                </div>
            </div>
        </li>
        <li>
            <div class="inner">
                <div class="bg"></div>
                <h2>image2</h2>
                <div class="direction">
                    <div class="title">bird2</div>
                    <div class="close"></div>
                </div>
            </div>
        </li>
        <li>
            <div class="inner">
                <div class="bg"></div>
                <h2>image3</h2>
                <div class="direction">
                    <div class="title">bird3</div>
                    <div class="close"></div>
                </div>
            </div>
        </li>
        <li>
            <div class="inner">
                <div class="bg"></div>
                <h2>image4</h2>
                <div class="direction">
                    <div class="title">bird4</div>
                    <div class="close"></div>
                </div>
            </div>
        </li>
        <li>
            <div class="inner">
                <div class="bg"></div>
                <h2>image5</h2>
                <div class="direction">
                    <div class="title">bird5</div>
                    <div class="close"></div>
                </div>
            </div>
        </li>
        <li>
            <div class="inner">
                <div class="bg"></div>
                <h2>image6</h2>
                <div class="direction">
                    <div class="title">bird6</div>
                    <div class="close"></div>
                </div>
            </div>
        </li>
    </ul>
</div>

行爲:

<script>
    (function () {
        var ul = document.querySelector('.init');
        var lis = document.querySelectorAll('li');
        var closes = document.querySelectorAll('.close');
        var last = null;
        // 點擊每個li打開
        lis.forEach((li, index) => {
            li.onclick = function (ev) {
                // 給ul加上動態的id
                ul.setAttribute('id', 'activeWrap');
                last && last.removeAttribute('class');
                li.setAttribute('class', 'activeLi');
                last = this;
            }

            closes[index].onclick = function (ev) {
                lis[index].className = '';
                ul.removeAttribute('id');
                last = null;
                ev.cancelBubble = true;
            }
        })

        //動畫顯示出來
        setTimeout(function () {
            ul.className = '';
        }, 200)

    })()
</script>

備註: 把圖片換掉就可以實現以下效果:
在這裏插入圖片描述
在這裏插入圖片描述

裏面帶有一些動畫,已經實現了,有興趣的自己學習一下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章