1. 程式人生 > 其它 >使用html+css實現的文字效果

使用html+css實現的文字效果

故障的文字風格

 

 

 

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
.text{
    display: inline-block;
    left: 400px;
    top: 100px;
    font-size: 3.5em;
    font-weight: 600;
    color: white;
    position: relative;
}
.text::before{
    content: attr(data-text);
    position: absolute;
    left: -2px;
    width: 100%;
    background: black;
    text-shadow:2px 0 red;
    animation: Fault-before 3s infinite linear alternate-reverse;
}
.text::after{
    content: attr(data-text);
    position: absolute;
    left: 2px;
    width: 100%;
    background: black;
    text-shadow: -2px 0 blue;
    animation: Fault-after 3s infinite linear alternate-reverse;
}


/*設定兩個偽元素before
after,分別定位到跟父元素同樣的位置,然後分別向左、右側移一點點的距離,
製作一個錯位的效果,然後都將背景色設定為與父元素背景色一樣的顏色,用於遮擋父元素*/
@keyframes Fault-before{ 0% { clip-path: inset(0 0 0 0); } 100% { clip-path: inset(.62em 0 .29em 0); } } @keyframes Fault-after{ 0% { clip-path: inset(0 0 0 0); } 100% { clip-path: inset(.29em 0 .62em 0); } }

主要是設定了兩個keyframes的動畫效果,分別為 animation-before 、animation-after,前者是準備給偽元素 before 使用的,後者是給偽元素 after 使用的。

其中clip-path屬性是CSS3的新屬性蒙版,其中的inset()值表示的是蒙版形狀為矩形,定義蒙版的作用區域後通過@keyframes來設定逐幀動畫,使蒙版的作用區域在垂直方向一直變化,實現上下抖動的效果。

 

    </style>
</head>
<body>
    <div class="text" data-text="軟體工程故障風格文字">
        軟體工程故障風格文字
    </div>
</body>
</html>