前端每日實戰:44# 視頻演示如何用純 CSS 創做背景色塊變換的按鈕特效

圖片描述

效果預覽

按下右側的「點擊預覽」按鈕能夠在當前頁面預覽,點擊連接能夠全屏預覽。css

https://codepen.io/comehope/pen/XYKdwghtml

可交互視頻教程

此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。前端

請用 chrome, safari, edge 打開觀看。git

https://scrimba.com/p/pEgDAM/cM7RLUGgithub

源代碼下載

每日前端實戰系列的所有源代碼請從 github 下載:chrome

https://github.com/comehope/front-end-daily-challengesdom

代碼解讀

定義 dom,標準導航結構下,<li> 中的每一個字母用 <span> 包裹:flex

<nav>
    <ul>
        <li>
            <span>p</span>
            <span>r</span>
            <span>o</span>
            <span>d</span>
            <span>u</span>
            <span>c</span>
            <span>t</span>
            <span>s</span>
        </li>
    </ul>
</nav>

居中顯示:動畫

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(to right bottom, dimgray, black);
}

定義文字樣式:spa

ul {
    padding: 0;
}

li {
    list-style-type: none;
}

li span {
    color: white;
    font-size: 50px;
    font-family: monospace;
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

當鼠標懸停時,用僞元素繪製傾斜的背景色塊:

li span {
    position: relative;
    transition: 500ms;
}

li:hover span {
    color: #333;
}

li span::before {
    content: '';
    position: absolute;
    height: 100%;
    width: 0;
    z-index: -1;
    transition: 500ms;
}

li:hover span::before {
    width: 70%;
    transform: rotate(-25deg);
    background: white;
}

設置緩動延時,增長動畫效果:

li:hover span {
    transition-delay: calc(80ms * var(--n) + 10ms);
}

li:hover span::before {
    transition-delay: calc(80ms * var(--n));
}

li span:nth-child(1) {
    --n: 1;
}

li span:nth-child(2) {
    --n: 2;
}

li span:nth-child(3) {
    --n: 3;
}

li span:nth-child(4) {
    --n: 4;
}

li span:nth-child(5) {
    --n: 5;
}

li span:nth-child(6) {
    --n: 6;
}

li span:nth-child(7) {
    --n: 7;
}

li span:nth-child(8) {
    --n: 8;
}

在單詞左側增長一條鼠標懸停時變亮的豎線:

li {
    padding-left: 2em;
    border: 2px solid transparent;
    border-left-color: silver;
    transition: 500ms;
}

li:hover {
    border-left-color: white;
}

dom 中增長几個按鈕,造成一組按鈕:

<nav>
    <ul>
        <li>
            <span>h</span>
            <span>o</span>
            <span>m</span>
            <span>e</span>
        </li>
        <li>
            <span>p</span>
            <span>r</span>
            <span>o</span>
            <span>d</span>
            <span>u</span>
            <span>c</span>
            <span>t</span>
            <span>s</span>
        </li>
        <li>
            <span>s</span>
            <span>e</span>
            <span>v</span>
            <span>i</span>
            <span>c</span>
            <span>e</span>
            <span>s</span>
        </li>
        <li>
            <span>c</span>
            <span>o</span>
            <span>n</span>
            <span>t</span>
            <span>a</span>
            <span>c</span>
            <span>t</span>
        </li>
    </ul>
</nav>

最後,文字居中對齊,再調整一下按鈕間距:

li {
    text-align: center;
    margin: 1em 0;
}

大功告成!

相關文章
相關標籤/搜索