多種方法實現Loading(加載)動畫效果

當咱們ajax提交一個按鈕的時候,給那個按鈕來個Loading效果會高端不少,體驗也會上升個層次。css

既能讓用戶知道正在提交中,也能防止二次提交,好處多多呢。git

上面的這個圈圈是會滾動的。簡單點的話,能夠直接用GIF動態圖片實現。不過如今已經有了CSS3和HTML5了,多了好幾種高大上的實現方式。github

這裏先來介紹幾個動畫的在線demo,第一個是HTML5 Boilerplate中的Effeckt.css,第二個是Animate.cssweb

下面一一列出,若是要結合按鈕的話,可自行修改下CSS或JS等文件。當要嵌入到實際項目中的時候,可能會改動一些地方,以實際狀況爲準了。ajax

 

1、PNG圖片+CSS3動畫

<div class="pull-up pull-up-loading">
  <span class="pull-icon"></span>正在載入中....
</div>
.pull-up-loading .pull-icon {
  background-position: 0 100%;
  /*chrome*/
  -webkit-transform: rotate(0deg) translateZ(0);
  -webkit-transition-duration: 0ms;
  -webkit-animation-name: loading;
  -webkit-animation-duration: 2s;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-timing-function: linear;
}
/*chrome*/

@-webkit-keyframes loading {
  from {
    -webkit-transform: rotate(0deg) translateZ(0);
  }
  to {
    -webkit-transform: rotate(360deg) translateZ(0);
  }
}

點擊查看在線實例:chrome

  1. 只有當加上pull-up-loading,纔會出現滾動
  2. 添加一個動畫keyframes,叫loading,是在作transform: rotate操做,下面的CSS省略了firefox中的動畫代碼,爲了看的清晰點,實例中有完整的firefox代碼

這裏有個在線生成Loading的純CSS代碼,cssload.net。樣式選擇仍是挺多的,就是對於老一點的瀏覽器的兼容性方面不是很強好比IE六、IE七、IE8等。canvas

再來幾個不一樣的款式:點擊可查看源碼瀏覽器

            

 

2、spin.js

  這是一個腳本文件。不依賴任何庫,能夠獨立執行,挺好用的,我在實際項目中使用過這個插件,當時我把全部的ajax提交都調用了這個插件,結合jQuery庫,作到Loading效果和防止二次提交。並且這個庫的瀏覽器兼容性很好,甚至兼容古老的IE6,並且不用引入額外的CSS或圖,可配置的參數也不少。app

  我粗略的看過代碼,標準的瀏覽器就用腳本寫CSS3來作動畫,對於古老點的瀏覽器就用setTimeout來模擬動畫。裏面還會初始化一個VML標籤,這個是針對IE的。動畫

  看代碼的時候看到了個頗有趣的符號「~~」,後面一查,說是將變量轉換成數字的一個方法,挺高級的。

  這個插件還提供了一個在線配置的小網站,點擊查看

showAjaxLoading: function(btn) {
    if (btn == null || btn == undefined || $(btn).length == 0) return;
    var left = $(btn).offset().left;
    var top = $(btn).offset().top;
    var width = $(btn).outerWidth();
    var height = $(btn).height();
    var opts = {
      lines: 9, // The number of lines to draw
      length: 0, // The length of each line
      width: 10, // The line thickness
      radius: 15, // The radius of the inner circle
      corners: 1, // Corner roundness (0..1)
      rotate: 0, // The rotation offset
      direction: 1, // 1: clockwise, -1: counterclockwise
      color: '#000', // #rgb or #rrggbb or array of colors
      speed: 1, // Rounds per second
      trail: 81, // Afterglow percentage
      shadow: false, // Whether to render a shadow
      hwaccel: false, // Whether to use hardware acceleration
      className: 'spinner', // The CSS class to assign to the spinner
      zIndex: 2e9, // The z-index (defaults to 2000000000)
      top: '50%', // Top position relative to parent
      left: '50%' // Left position relative to parent
    };
    $('#ajax_spin').remove();
    $('body').append('<div id="ajax_spin" style="position:absolute;background:#FFF;filter:alpha(opacity=30);opacity:0.3"><div id="ajax_spin_inner" style="position:relative;height:50px;"></div></div>');
    $('#ajax_spin').css({
      'top': top,
      'left': left,
      'width': width,
      'height': height
    });
    var target = document.getElementById('ajax_spin_inner');
    var spinner = new Spinner(opts).spin(target);
    //return spinner;
  },
  stopAjaxLoading: function() {
    $('#ajax_spin').remove();
    //new Spinner(opts).spin(target)
    //spinner.stop();
  }

 

上面那段代碼是我在一個實際項目中寫的,就是顯示和移除Loading效果,而且在按鈕上面覆蓋這層效果防止二次提交。

  1. btn就是按鈕jQuery對象
  2. left,top找到按鈕的左右位移,width和height獲取按鈕的寬和高,width用的是outerWidth
  3. $('body')加入一個可以覆蓋按鈕的層
  4. 初始化一個Spinner對象,並加入到那個覆蓋層中

 

3、Ladda

能夠單獨使用,或者結合上面的插件spin一塊兒結合使用。demo頁面的效果挺高大上的,但用到實例可能仍是須要些修改的。

點擊查看主頁

下圖隨便選了幾個例子,能夠實現不一樣尺寸的按鈕大小,不一樣方向的滾動,將按鈕變成原型,或帶進度條的按鈕。挺多樣性的。

點擊查看demo頁面

 

HTML代碼以下:

<button class="ladda-button" data-style="expand-right"><span class="ladda-label">Submit</span></button>
// Automatically trigger the loading animation on click
Ladda.bind( 'input[type=submit]' );

// Same as the above but automatically stops after two seconds
Ladda.bind( 'input[type=submit]', { timeout: 2000 } );

結構看上去不是很複雜,JS腳本的引入也不是很難。不過在引入實際項目中確定仍是須要作些修改的。

相比spin插件,這插件要引入的文件就多了,不但要引入JS還要引入CSS。

  點擊查看codepen上覆制的代碼

  我原本想在codepen頁面中,把demo頁面重現一次,在把github裏面的dist/CSS/ladda.min.css文件複製到codepen中,JS中的ladda.js和spin.js也複製過來。發生了點意外,那個滾動條總是會往下面一點。CSS都是所有複製的,很奇怪。後面發現是CSS的問題,真的是實際應用一下才會看到具體狀況。

  

  

  demo頁面的CSS:

.ladda-button .ladda-spinner {
  position: absolute;
  z-index: 2;
  display: inline-block;
  width: 32px;
  height: 32px;
  top: 50%;
  margin-top: -17px;
  opacity: 0;
  pointer-events: none
}

  Github上的CSS:區別就是margin-top的不同。

.ladda-button .ladda-spinner {
  position: absolute;
  z-index: 2;
  display: inline-block;
  width: 32px;
  height: 32px;
  top: 50%;
  margin-top: 0;
  opacity: 0;
  pointer-events: none
}

 

4、Sonic.js

這個插件是建立一個canvas畫布來實現Loaing動畫效果。 款式也比較多,以下圖所示:點擊查看在線demo

在線demo中還展現了用CSS3動畫+CSS Sprite技術實現動畫

 

點擊查看Github主頁

對於較老版本的瀏覽器,sonic也作了處理,能將canvas轉換成GIF圖片。點擊查看SonicGIF

相關文章
相關標籤/搜索