關於CSS3的animate如何?“...”loading動畫效果(二)

來源:互聯網
上載者:User

box-shadow實現的打點效果

簡介

box-shadow理論上可以產生任意的圖形效果,當然也就可以實現點點點的loading效果了。

實現原理

html代碼,首先需要寫如下html代碼以及class類名:

訂單提交中<span class="dotting"></span>

css代碼

.dotting {    display: inline-block; min-width: 2px; min-height: 2px;    box-shadow: 2px 0 currentColor, 6px 0 currentColor, 10px 0 currentColor; /* for IE9+, ..., 3個點 */    animation: dot 4s infinite step-start both; /* for IE10+, ... */    *zoom: expression(this.innerHTML = '...'); /*  for IE7. 若無需相容IE7, 此行刪除 */}.dotting:before { content: '...'; } /* for IE8. 若無需相容IE8, 此行以及下一行刪除*/.dotting::before { content: ''; } /* for IE9+ 覆蓋 IE8 */:root .dotting { margin-right: 8px; } /* for IE9+,FF,CH,OP,SF 佔據空間*/@keyframes dot {    25% { box-shadow: none; }                                  /* 0個點 */    50% { box-shadow: 2px 0 currentColor; }                    /* 1個點 */    75% { box-shadow: 2px 0 currentColor, 6px 0 currentColor;  /* 2個點 */ }}

這裡用到了currentColor這個關鍵字,IE9+瀏覽器支援,其可以讓CSS產生的圖形的顏色跟所處環境的color屬性值一樣,也就是跟文字顏色一樣。

各瀏覽器實現的效果:

不足之處

雖然幾乎所有瀏覽器都有模有樣,但是,從效果上講,還是有瑕疵的,IE10+以及FireFox瀏覽器下的點的邊緣有些虛(參見下),雖然CSS代碼並沒有設定盒陰影模糊。這種羽化現象可以讓IE以及FireFox在大數值盒陰影時候效果更接近photoshop的陰影製作效果;但是,在小尺寸陰影時候,並不是我們想要的。

border + background實現的打點效果

實現原理

html代碼

訂單提交中<span class="dotting"></span>

css代碼

.dotting {    display: inline-block; width: 10px; min-height: 2px;    padding-right: 2px;    border-left: 2px solid currentColor; border-right: 2px solid currentColor;       background-color: currentColor; background-clip: content-box;    box-sizing: border-box;    animation: dot 4s infinite step-start both;    *zoom: expression(this.innerHTML = '...'); /* IE7 */}.dotting:before { content: '...'; } /* IE8 */.dotting::before { content: ''; }:root .dotting { margin-left: 2px; padding-left: 2px; } /* IE9+ */@keyframes dot {    25% { border-color: transparent; background-color: transparent; }          /* 0個點 */    50% { border-right-color: transparent; background-color: transparent; }    /* 1個點 */    75% { border-right-color: transparent; }                                   /* 2個點 */}

說明:

1.同樣是4秒動畫,每秒鐘顯示1個點;
2.IE7/IE8實現原理跟上面box-shadow方法一致,都是內容產生,如果無需相容IE7/IE8, 可以按照第一個例子CSS代碼注釋說明刪除一些CSS;
3.currentColor關鍵字可以讓圖形字元化,必不可少;
4.最大功臣是CSS3 background-clip屬性,可以讓IE9+瀏覽器下左右padding沒有背景色,於是形成了等分打點效果。
5.box-sizing是讓現代瀏覽器和IE7/IE8佔據寬度完全一樣的功臣:IE7/IE8實際寬度是width+padding-right為12像素,其他現代瀏覽器為width+margin-left也是12像素;
6.這裡CSS代碼主要用來展示原理,故沒有顯示-webkit-animation以及@-webkit-keyframes私人首碼,實際目前還是需要的;

相關文章

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.