自定義漢堡按鈕

效果圖如下

hanbao.gif

首先是HTML部分的代碼,很簡單。

<div class="hamburger">
        <span class="line"></span>
        <span class="line"></span>
        <span class="line"></span>
    </div>

接下來就是重點了,CSS代碼部分走起。

<style>
        .hamburger { /*這個不重要,可有可無 主要是針對導(dǎo)航欄布局時,漢堡按鈕的位置*/
            padding-top: 6px;
            -webkit-transition: all 0.3s ease-in-out;
            -o-transition: all 0.3s ease-in-out;
            transition: all 0.3s ease-in-out;
        }
        .hamburger .line { /*定義三條紅線,加上過渡,準備動*/
            width: 30px;
            height: 2px;
            background-color: #fe1111;
            display: block;
            margin: 8px auto;
            -webkit-transition: all 0.3s ease-in-out;
            -o-transition: all 0.3s ease-in-out;
            transition: all 0.3s ease-in-out;
        }
        .is-active { /*通過js添加類名,給漢堡按鈕加上縮放動畫*/
            animation: smallbig 0.6s forwards;
        }
        .is-active .line:nth-child(1),
        .is-active .line:nth-child(2),
        .is-active .line:nth-child(3) { /*三條線加點延時,沒什么卵用*/
            -webkit-transition-delay: 0.2s;
            -o-transition-delay: 0.2s;
            transition-delay: 0.2s;
        }
    
        .is-active .line:nth-child(2) { /*延時0.2s之后,進行過渡動畫,第二句話是做兼容處理*/
            opacity: 0;
            filter: alpha(opacity=0);
        }
        .is-active .line:nth-child(1) {  /*第一條線位移到中心位置,順時針旋轉(zhuǎn)45°*/
            -webkit-transform: translateY(8px) rotate(45deg);
            -ms-transform: translateY(8px) rotate(45deg);
            -o-transform: translateY(8px) rotate(45deg);
            transform: translateY(8px) rotate(45deg);
        }
        .is-active .line:nth-child(3) { /*第三條線位移到中心位置,逆時針旋轉(zhuǎn)45°*/
            -webkit-transform: translateY(-12px) rotate(-45deg);
            -ms-transform: translateY(-12px) rotate(-45deg);
            -o-transform: translateY(-12px) rotate(-45deg);
            transform: translateY(-12px) rotate(-45deg);
        }
        @keyframes smallbig { /*定義縮放的動畫,50%的時候縮放為0*/
            0%,
            100% {
                -webkit-transform: scale(1);
                -ms-transform: scale(1);
                -o-transform: scale(1);
                transform: scale(1);
            }
            50% {
                -webkit-transform: scale(0);
                -ms-transform: scale(0);
                -o-transform: scale(0);
                transform: scale(0);
            }
        }
    </style>

最后,來一個簡單的js,搞定。

<script>
        $(document).ready(function () {
            $(".hamburger").click(function () {
                $(this).toggleClass("is-active");
            });
        });
    </script>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容