前端每日實戰:34# 視頻演示如何用純 CSS 創做在文本先後穿梭的邊框

圖片描述

效果預覽

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

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

可交互視頻教程

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

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

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

源代碼下載

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

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

代碼解讀

定義 dom,容器中包含文本:flex

<div class="warning">ERROR 404</div>

居中顯示:spa

body {
  margin: 0;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgb(20%, 20%, 20%);
}

定義文字樣式:code

.warning {
    color: whitesmoke;
    font-size: 100px;
    font-family: sans-serif;
    font-weight: bold;
}

用僞元素定義邊框尺寸:

.warning {
    position: relative;
    padding: 0.6em 0.4em;
}

.warning::before,
.warning::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border: 0.2em solid;
    box-sizing: border-box;
}

把邊框分爲兩部分,拼在一塊兒:

.warning::before,
.warning::after {
    border: 0.2em solid transparent;
    color: orangered;
}

.warning::before {
    border-top-color: currentColor;
    border-right-color: currentColor;
}

.warning::after {
    border-bottom-color: currentColor;
    border-left-color: currentColor;
}

把上邊框和右邊框下沉一層:

.warning::before {
    z-index: -1;
}

爲下邊框和在邊框加上陰影:

.warning::after {
    box-shadow: 0.3em 0.3em 0.3em rgba(20%, 20%, 20%, 0.8);
}

最後,讓邊框轉起來:

.warning::before,
.warning::after {
    animation: rotating 10s infinite;
}

@keyframes rotating {
    to {
        transform: rotate(360deg);
    }
}

大功告成!

相關文章
相關標籤/搜索