1. 程式人生 > >72.純 CSS 創作氣泡填色的按鈕特效

72.純 CSS 創作氣泡填色的按鈕特效

blank one delay adding segment tran hover borde pac

原文地址:https://segmentfault.com/a/1190000015560736

感想:過渡效果+xyz中一軸。

HTML code:

<nav>
    <ul>
        <li>
            home
            <span></span><span></span><span></span><span></span>
        </li>
    </ul>
</nav>

CSS code:

html, body,ul {
    margin: 0;
    padding: 0;
}
/* 設置所有元素的width、height包含border、padding、content
*{
    box-sizing: border-box;
}
 */
/* 設置body的子元素水平垂直居中 */
body {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: black;
}
/*
設置按鈕樣式*/ nav ul { list-style-type: none; } nav ul li{ /* 這裏的相對定位為li絕對定位做準備 */ position: relative; --c: goldenrod; color: var(--c); font-size: 16px; font-weight: bold; font-family: sans-serif; width: 12em; height: 3em; line-height: 3em; border
: 0.3em solid var(--c); border-radius: 0.5em; text-align: center; letter-spacing: 0.1em; /* 外面的隱藏 */ overflow: hidden; /* 顯示層級降低,讓文字優先顯示 */ z-index: 1; } /* 畫出氣泡,4個並列擺放 */ nav ul li span{ position: absolute; left: calc((var(--n) - 1) * 25%); width: 25%; height: 100%; border-radius: 50%; background-color: var(--c); /* 定義Y軸位置 */ transform: translateY(150%); /* 過渡屬性 */ transition: 0.5s; transition-delay: calc((var(--n) - 1) * 0.1s); /* 層級降低 */ z-index: -1; } nav ul li span:nth-child(1) { --n: 1; } nav ul li span:nth-child(2) { --n: 2; } nav ul li span:nth-child(3) { --n: 3; } nav ul li span:nth-child(4) { --n: 4; } nav ul li:hover{ color: black; } /* 鼠標移到按鈕上 */ nav ul li:hover span { transform: translateY(0) scale(2); }

72.純 CSS 創作氣泡填色的按鈕特效