html&css 展開收縮的圖標導航

演示效果

動畫.gif

實現(xiàn)思路

采用單選按鈕的狀態(tài)來控制導航的展開和收縮,通過transform來實現(xiàn)變換,并添加過渡效果來美化展開和收縮。
單選按鈕要去除默認效果,設置z-index使其在最上層蓋住其他部分。

代碼

html結構

<!--
 * @Descripttion: 
 * @version: 
 * @Author: siebe
 * @Date: 2022-07-16 00:04:01
 * @LastEditors: siebe
 * @LastEditTime: 2022-07-16 00:04:17
-->
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>展開收縮的圖標導航</title>
    <script src="https://kit.fontawesome.com/d0ccd97f28.js" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class="menu">
        <input type="checkbox">
        <button class="fa-solid fa-pen"></button>
        <button class="fa-solid fa-arrows-rotate"></button>
        <button class="fa-solid fa-trash-can"></button>
        <button class="fa-solid fa-bars"></button>
    </div>

</body>

</html>

css樣式

body {
    margin: 0;
    padding: 0;
    height: 100vh;
    background-color: #c4c2c2;

    display: flex;
    justify-content: center;
    align-items: center;
}

.menu {
    position: relative;
}

input {
    margin: 0;
    padding: 0;
    position: absolute;
    top: 0px;
    right: 5px;
    z-index: 5;
    -moz-appearance: none;
    -webkit-appearance: none;
    appearance: none;
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    outline: 0;
    cursor: pointer;
}

button {
    height: 40px;
    width: 40px;
    margin-right: 5px;
    padding: 0;
    display: inline-block;
    background-color: #E08E79;
    border: 0;
    outline: 0;
    border-radius: 50%;
    color: white;
    text-align: center;
    cursor: pointer;
    will-change: transform;
    transition: background-color 250ms cubic-bezier(0.41, 0.29, 0.38, 0.94), transform 250ms cubic-bezier(0.41, 0.29, 0.38, 0.94);
}

button:hover {
    background-color: #d66b50;
}

button:active {
    background-color: #c54d2e;
}

input:hover~.fa-bars {
    background-color: #d66b50;
}

input~button:nth-child(4) {
    transform: translateX(50px) rotate(60deg);
}

input~button:nth-child(3) {
    transform: translateX(100px) rotate(90deg);
}

input~button:nth-child(2) {
    transform: translateX(150px) rotate(180deg);
}

input[type="checkbox"]:checked~button {
    transform: translateX(0);
}

gitee地址:siebe/html-css-demo (https://gitee.com/siebe/html-css-demo)
圖標來源:圖標庫Font Awesome中的免費圖標。

每天一個網(wǎng)頁小實例,生活多姿又多彩。

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

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

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