模仿Apple官網(wǎng)HamburgerMenu點(diǎn)擊特效

案例來源

本例來自CodingStartup的視頻:使用 HTML+CSS 制作動態(tài) Hamburger Menu

學(xué)到知識

  1. 通過checkbox的偽類checked,來綁定菜單,實現(xiàn)點(diǎn)擊checkbox實現(xiàn)特效。
  2. 使用label嵌套菜單div,并使用for屬性綁定checkbox,實現(xiàn)點(diǎn)擊菜單本身即可實現(xiàn)特效。
  3. 在使用transition時,要設(shè)置元素的初始狀態(tài),例如本例的top屬性,否則會出現(xiàn)異常的效果。
  4. 使用transform-origin設(shè)置元素原點(diǎn),表示從哪個位置進(jìn)行改變。

主要代碼

html

<body>
    <header>
        <input type="checkbox" id="toggle">
        <label for="toggle">
                <div class="hamburger-container">
                <span></span>
                <span></span>
            </div>
        </label>

        <div class="nav-item">
            <ul>
                <li>Home</li>
                <li>iPhone</li>
                <li>iPad</li>
                <li>Mac</li>
                <li>TV</li>
            </ul>
        </div>
    </header>
</body>

css

#toggle {
    display: none;
}

.hamburger-container {
    display: block;
    width: 20px;
    height: 20px;
    position: relative;
    top: 16px;
}

.hamburger-container span {
    display: block;
    height: 1px;
    background-color: #fff;
    position: relative;
    transition: top .3s ease-in-out .3s, transform .3s ease-in-out;
    top: 0;
}

.hamburger-container span:nth-child(2) {
    margin-top: 7px;
}

header {
    height: 44px;
    background-color: #000;
    padding: 0 18px;
}

body {
    margin: 0;
    padding: 0;
}

#toggle:checked + label .hamburger-container span:nth-child(1) {
    transform: rotate(45deg);
    top: 4px;
    transition: transform .3s ease-in-out .3s, top .3s ease-in-out;
}

#toggle:checked + label .hamburger-container span:nth-child(2) {
    transform: rotate(-45deg);
    top: -4px;
    transition: transform .3s ease-in-out .3s, top .3s ease-in-out;
}

.nav-item {
    background-color: rgba(0, 0, 0, .82);
    width: 100%;
    height: 100vh;
    position: absolute;
    top: 44px;
    left: 0;
    transform: scaleY(0);
    transition: transform .3s ease-in-out, opacity .3s ease-in-out;
    transform-origin: 50% 0;
    opacity: 0;
}

#toggle:checked ~ .nav-item {
    transform: scale(1);
    opacity: 1;
}

ul {
    margin-top: 1em;
}

ul li {
    color: #fff;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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