首頁  >  文章  >  web前端  >  如何使用純CSS實現文字斷開的動畫效果(附源碼)

如何使用純CSS實現文字斷開的動畫效果(附源碼)

不言
不言原創
2018-08-22 10:37:022254瀏覽

這篇文章帶給大家的內容是關於如何使用純CSS實現文字斷開的動畫效果(附源碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

效果預覽

如何使用純CSS實現文字斷開的動畫效果(附源碼)

原始碼下載

https://github.com/comehop​​e/front- end-daily-challenges/tree/master/012-broken-text-effects

#程式碼解讀

定義dom,只有一個元素,元素有一個data-text 屬性,屬性值等於元素內的文字:

<div class="text" data-text="BREAK">BREAK</div>

居中顯示:

html, body {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

設定漸層背景色:

body {
    background: linear-gradient(brown, sandybrown);
}

設定文字的字型字號:

.text {
    font-size: 5em;
    font-family: "arial black";
}

#利用偽元素增加文字:

.text {
    position: relative;
}

.text::before,
.text::after {
    content: attr(data-text);
    position: absolute;
    top: 0;
    left: 0;
    color: lightyellow;
}

設定左側文字的遮罩:

.text::before {
    background-color: darkgreen;
    clip-path: polygon(0 0, 60% 0, 30% 100%, 0 100%);
}

設定右側文字的背景和遮罩:

.text::after {
    background-color: darkblue;
    clip-path: polygon(60% 0, 100% 0, 100% 100%, 30% 100%);
}

當滑鼠劃過時,遮罩的文字分別向兩側偏移:

.text::before,
.text::after {
    transition: 0.2s;
}

.text:hover::before {
    left: -0.15em;
}

.text:hover::after {
    left: 0.15em;
}

隱藏輔助元素,包括原始文字和偽元素的背景色:

.text {
    color: transparent;
}

.text::before {
    /*background-color: darkgreen;*/
}

.text::after {
    /*background-color: darkblue;*/
}

兩側文字增加歪斜效果:

.text:hover::before {
    transform: rotate(-5deg);
}

.text:hover::after {
    transform: rotate(5deg);
}

微調文字的高度:

.text:hover::before {
    top: -0.05em;
}

.text:hover::after {
    top: 0.05em;
}

大功告成!

相關推薦:

如何使用CSS實現漸層色動畫邊框的效果(附程式碼)

如何使用CSS與混色模式實作loader動畫效果(附程式碼)

#

以上是如何使用純CSS實現文字斷開的動畫效果(附源碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn