利用CSS3實現文字向右迴圈的閃過效果

來源:互聯網
上載者:User
這篇文章主要跟大家分享了利用純CSS3實現文字向右迴圈閃過效果的相關資料,因為相容性的問題,常被用於移動端,實現的效果非常不錯,文中給出了詳細的介紹和範例程式碼,需要的朋友們下面來一起看看吧。

本文介紹的利用純CSS3實現文字向右迴圈閃過效果的相關資料,下面話不多說,大家先來看看範例程式碼吧。

範例程式碼:

<!DOCTYPE html><html lang="en">    <head>        <meta charset="utf-8">        <style>            .shadow {                text-align: center;                /* 背景顏色線性漸層 */                    /* 老式寫法 */                        /* linear為線性漸層,也可以用下面的那種寫法。left top,right top指的是漸層方向,左上到右上 */                        /* color-stop函數,第一個表示漸層的位置,0為起點,0.5為中點,1為結束點;第二個表示該點的顏色。所以本次漸層為兩邊灰色,中間漸白色 */                background: -webkit-gradient(linear, left top, right top, color-stop(0, #4d4d4d), color-stop(.4, #4d4d4d), color-stop(.5, white), color-stop(.6, #4d4d4d), color-stop(1, #4d4d4d));                    /* 新式寫法 */                /* background: -webkit-linear-gradient(left top, right top, color-stop(0, #4d4d4d), color-stop(.4, #4d4d4d), color-stop(.5, white), color-stop(.6, #4d4d4d), color-stop(1, #4d4d4d)); */                /* 設定為text,意思是把常值內容之外的背景給裁剪掉 */                -webkit-background-clip: text;                /* 設定對象中的文字填充顏色 這裡設定為透明 */                -webkit-text-fill-color: transparent;                /* 每隔2秒調用下面的CSS3動畫 infinite屬性為迴圈執行animate */                -webkit-animation: animate 1.5s infinite;            }            /* 相容寫法,要放在@keyframes前面 */            @-webkit-keyframes animate {                /* 背景從-100px的水平位置,移動到+100px的水平位置。如果要移動Y軸的,設定第二個數值 */                from {background-position: -100px;}                to {background-position: 100px;}            }            @keyframes animate {                from {background-position: -100px;}                to {background-position: 100px;}            }        </style>    </head>    <body>        <p class="shadow">文字向右閃過效果</p>    </body></html>

下面這是:

這個白色漸層閃過效果用CSS3做很容易也很方便,唯一不好的地方應該就是相容問題了。所以現在一般都用在移動端上面了。

來啦來啦!(敲黑板) 我覺得代碼注釋已經比較清楚了,所以畫畫重點就好了!!!

1、infinite 這是迴圈執行的屬性,有了它,才能一閃一閃滴!

2、-webkit-text-fill-color: transparent; 文字填充顏色為透明,沒有設定的話,看不出白色漸層划過效果的!

3、-webkit-background-clip: text; 把常值內容之外的背景給裁剪掉,如果不加,文字顯示不出來,只顯示漸層的顏色!

4、color-stop() 漸層的color-stop 函數,表示漸層的位置和顏色,就是它,我們才能想在哪裡漸層就哪裡漸層,再讓它移動起來,就出現一閃一閃的效果了!

最後,說一下思路:

  首先,設定一個中間白色、兩邊灰色的漸層背景色;

  其次,文字填充顏色設為透明(才能看到白色背景);

  接著,把文字之外的背景色給裁剪掉(只顯示文字);

  最後,用@keyframes,讓背景白色位置迴圈從左至右執行。

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.