Bootstrap 輪播加上css3動畫,炫酷到底!

對於網站開發者來講,對展現內容增長一個滑動或者是輪播效果的是很是常見的需求。收費和免費的輪播插件多的是不勝枚舉。其中不少提供不少有用的配置選項和動態效果。javascript

不少時候,若是你的項目須要的是一個輕量級的輪播,不須要不少的功能。同時你的項目是採用Bootstrap,(一個最流行的開源前端框架)的話。你能夠參考一下bootstrap官方組件。css

這篇文章將展現如何在 Bootstrap輪播上添加有趣的動畫效果。同時確保這個js組件自由擴展和快速上手。html

 

介紹Animate.css

爲了讓我本身寫的動畫效果值得稱讚,我用一個很是有名的開源的CSS3動畫庫,被形象的稱爲animate.css。 Dan Eden寫的。前端

這是讓我能專一於手頭的任務,而不是解釋CSS3動畫的代碼。然而,若是你想深刻的瞭解,你能夠在SitePoint上Craig Buckler寫的CSS3動畫系列進一步學習。java

 

用Animate.css 須要2個步驟:

  1. 在html文檔中引入animate.min.css。
  2. 在網頁中要加動畫的元素上添加animated yourchosenanimation類。

接下來你用Animate.css網站上的看到的關於動畫的類名,代替yourchosenanimation。css3

 

引入Bootstrap輪播組件

Bootstrap輪播組件有三個主要的部分。web

  • 輪播指示顯示幻燈的頁面數量,給用戶提供一個視覺線索,並提供能夠滑動的導航。
  • 輪播條目,一個叫.carousel-inner的類,包含在外邊框的裏邊。表明每個獨立的滑塊。每一個圖片裏邊的均可以放置圖片。也能夠添加標題。還能夠在html元素上添加carousel-caption類名。Bootstrap會有自帶的樣式。咱們能夠經過這些元素添加動畫。
  • 最後,是輪播控制箭頭,功能是可使用戶先後滑動。

若是想了解更多Bootstrap輪播組件的詳情,能夠查看Syed Fazle Rahman的用Bootstrap3建立js輪播效果這篇文章。bootstrap

爲了簡單的展現demo,就先不加圖片了。焦點先放在輪播框架上做爲動畫。瀏覽器

 

構建HTML結構

下邊是你須要引用到你項目當中的:緩存

  • jQuery
  • Bootstrap’s CSS and JavaScript
  • Animate.css
  • 一個樣式表和js文檔。

爲了加快開發進程,從Bootstrap官網引用了模板和必要的文件。

下邊是Bootstrap輪播代碼:

  1. <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  2. <!-- Indicators -->
  3. <ol class="carousel-indicators">
  4. <li data-target="#carousel-example-generic" data-slide-to="0" class="active">
  5. </li>
  6. <li data-target="#carousel-example-generic" data-slide-to="1"></li>
  7. <li data-target="#carousel-example-generic" data-slide-to="2"></li>
  8. </ol>
  9. <!-- Wrapper for slides -->
  10. <div class="carousel-inner" role="listbox">
  11.  
  12. <!-- First slide -->
  13. <div class="item active">
  14. <div class="carousel-caption">
  15. <h3 data-animation="animated bounceInLeft">
  16. This is the caption for slide 1
  17. </h3>
  18. <h3 data-animation="animated bounceInRight">
  19. This is the caption for slide 1
  20. </h3>
  21. <button class="btn btn-primary btn-lg"
  22. data-animation="animated zoomInUp">Button</button>
  23. </div>
  24. </div><!-- /.item -->
  25.  
  26. <!-- Second slide -->
  27. <div class="item">
  28. <div class="carousel-caption">
  29. <h3 class="icon-container" data-animation="animated bounceInDown">
  30. <span class="glyphicon glyphicon-heart"></span>
  31. </h3>
  32. <h3 data-animation="animated bounceInUp">
  33. This is the caption for slide 2
  34. </h3>
  35. <button class="btn btn-primary btn-lg"
  36. data-animation="animated zoomInRight">Button</button>
  37. </div>
  38. </div><!-- /.item -->
  39.  
  40. <!-- Third slide -->
  41. <div class="item">
  42. <div class="carousel-caption">
  43. <h3 class="icon-container" data-animation="animated zoomInLeft">
  44. <span class="glyphicon glyphicon-glass"></span>
  45. </h3>
  46. <h3 data-animation="animated flipInX">
  47. This is the caption for slide 3
  48. </h3>
  49. <button class="btn btn-primary btn-lg"
  50. data-animation="animated lightSpeedIn">Button</button>
  51. </div>
  52. </div><!-- /.item -->
  53. </div><!-- /.carousel-inner -->
  54.  
  55. <!-- Controls -->
  56. <a class="left carousel-control" href="#carousel-example-generic"
  57. role="button" data-slide="prev">
  58. <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
  59. <span class="sr-only">Previous</span>
  60. </a>
  61.  
  62. <a class="right carousel-control" href="#carousel-example-generic"
  63. role="button" data-slide="next">
  64. <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
  65. <span class="sr-only">Next</span>
  66. </a>
  67.  
  68. </div><!-- /.carousel -->
 

若是以上代碼沒有錯,你在瀏覽器打開會看到一個能夠運行的輪播,上邊的一切不包含一行javascript代碼。若是你不添加任何圖像,

只是在css文檔給.carousel .item這個類塊添加min-height值防止輪播塌陷。

在輪播標題內的元素添加一個動畫屬性data-animation,用這個特別的動畫類庫做爲他們的值。

若是你想從Animate.css庫體驗其餘的動畫,用你選擇的動畫類名代替data-animation屬性值。

咱們在javascript代碼中用data-animation屬性值。

雖然一個簡單的自動輪播在一些案例中能夠找到,可是對於這個案例咱們有更多的控制。

在這個方向的第一步,從元素中刪除data-ride="carousel"值,把data-ride屬性值初始化兒不用寫任何代碼。可是,咱們打算用js代

碼控制輪播,所以,這個data-ride屬性就沒必要要了。

 

給輪播加CSS樣式

如今根據本身的喜愛,發揮創造力給輪播標題添加樣式。我將要寫的樣式規則是能順暢工做的demo。

更具體的說,咱們增長動畫延遲屬性的控制。定義每一個動畫何時開始(注意爲了簡單演示,省略了瀏覽器前綴)

  1. .carousel-caption h3:first-child {
  2. animation-delay: 1s;
  3. }
  4.  
  5. .carousel-caption h3:nth-child(2) {
  6. animation-delay: 2s;
  7. }
  8.  
  9. .carousel-caption button {
  10. animation-delay: 3s;
  11. }

上面的代碼片斷中確保元素動畫有序開始。還能夠作其餘的效果。例如,你能夠選擇前兩個標題同時出現。而後是button按鈕。能夠本身決定,享受樂趣吧。

 

寫jQuery代碼:

咱們開始初始化這個輪播,在你的自定義的javascript 文件中添加一下代碼:

  1. var $myCarousel = $('#carousel-example-generic');
  2. // Initialize carousel
  3. $myCarousel.carousel();

咱們已經動態的設置了輪播,接下來,咱們來解決這個動畫。

爲了使第一個幻燈片的標題有動畫,當頁面在瀏覽器加載完後腳本得運行。隨後的幻燈片在動畫下進入到咱們的視野,咱們的代碼在slide.bs.carousel 事件上運行。意味着一樣的代碼運行兩次:頁面加載一次和slide.bs.carousel 事件一次。

由於咱們喜歡遵循不重複的原則,咱們打算把咱們的代碼封裝在函數中,並在適當的時候引用。

代碼:

  1. function doAnimations(elems) {
  2. var animEndEv = 'webkitAnimationEnd animationend';
  3. elems.each(function () {
  4. var $this = $(this),
  5. $animationType = $this.data('animation');
  6. // Add animate.css classes to
  7. // the elements to be animated
  8. // Remove animate.css classes
  9. // once the animation event has ended
  10. $this.addClass($animationType).one(animEndEv, function () {
  11. $this.removeClass($animationType);
  12. });
  13. });
  14. }
  15. // Select the elements to be animated
  16. // in the first slide on page load
  17. var $firstAnimatingElems = $myCarousel.find('.item:first')
  18. .find('[data-animation ^= "animated"]');
  19. // Apply the animation using our function
  20. doAnimations($firstAnimatingElems);
  21. // Pause the carousel
  22. $myCarousel.carousel('pause');
  23.  
  24. // Attach our doAnimations() function to the
  25. // carousel's slide.bs.carousel event
  26. $myCarousel.on('slide.bs.carousel', function (e) {
  27. // Select the elements to be animated inside the active slide
  28. var $animatingElems = $(e.relatedTarget)
  29. .find("[data-animation ^= 'animated']");
  30. doAnimations($animatingElems);
  31. });

上邊的代碼 咱們來分析一下。

 

來看doAnimations()函數

這個doAnimations() 函數執行的任務以下。

它開始經過緩存變量中含有的animationend事件名稱的字符串。這個事件告訴咱們,你可能已經猜到,當每一個動畫結束。咱們須要這個點的信息,由於每一次的動畫結束後,咱們將animate.css類移除。若是咱們不作移除,輪播的標題將只有一次動畫,也就是,只是在第一次輪播顯示特定的幻燈片。

  1. var animEndEv = 'webkitAnimationEnd animationend';

接來下,咱們的函數循環遍歷每個咱們想要有動畫的元素,並獲取data-animation的屬性值。想上邊所說的,這個值包含咱們想要添加給元素的Animate.css類,以便有動畫效果。

  1. elems.each(function () {
  2. var $this = $(this),
  3. $animationType = $this.data('animation');
  4. // etc...
  5. });

最後,這個doAnimations() 函數動態添加animate.css類的每一個要執行動畫的元素上,當動畫結束的時候,還附加了一個事件監聽。動畫結束後咱們移除從Animate.css添加的類。這樣確保下一個輪播燈片回到當前的區域。(你試着刪除這段代碼,看看會發生什麼)

  1. $this.addClass($animationType).one(animEndEv, function () {
  2. $this.removeClass($animationType);
  3. });

 

第一個標題的動畫

當頁面在瀏覽器中加載時,咱們在第一個幻燈片中動畫的內容:

  1. var $firstAnimatingElems = $myCarousel.find('.item:first')
  2. .find("[data-animation ^= 'animated']");
  3. doAnimations($firstAnimatingElems);

在這個代碼中,咱們找到第一張燈片,咱們但願經過使用data-animation從動畫的標題獲取動畫屬性的值。而後咱們把變量 $firstAnimatingElems 當作參數傳給doAnimations()函數,而後執行函數。

 

輪播的中止功能

當第一張燈片內容執行完動畫之後,咱們中止這個輪播功能。

  1. $myCarousel.carousel('pause');

這是Bootstrap輪播組件防止不停旋轉的特徵。不停的旋轉,可能會讓訪客生厭。

在這種狀況下,我建議確保輪播不直接循環到下一個燈片直到全部的動畫運行完畢。能夠經過設置在初始化代碼中的「間隔」選項來控制這個:

  1. $myCarousel.carousel({
  2. interval: 4000
  3. });

在我看來,一個無限循環輪播標題跳躍每一次的滑動進入視線不理想。

 

輪播幻燈片標題的動畫

爲每張幻燈片的動畫輪播標題變得可見須要如下描述的步驟。

首先,咱們在slide.bs.carousel上添加一個事件監聽器。

當幻燈片實例方法被調用時,該事件當即觸發。

  1. $myCarousel.on('slide.bs.carousel', function (e) {
  2. // do stuff...
  3. });

接下來,咱們選擇當前的燈片,找到咱們但願增長動畫的元素。下邊的代碼用了slide.bs.carousel事件的.relatedTarget屬性來綁定動畫。

  1. var $animatingElems = $(e.relatedTarget).find("[data-animation ^= 'animated']");

最後,咱們調用doAnimations()函數,把$animatingElems當作參數傳進去。

  1. doAnimations($animatingElems);
 

總體的demo:

demo

 

結論:

正如大家許多人可能知道,輪播有一些須要開發者考慮的問題。

在這篇文章中,我展現瞭如何添加一些額外的精力,用幾行jQuery和animate.css庫用在基本的Bootstrap輪播組件。然而,其餘相似的css庫。或者css3動畫,咱們會作的同樣好。

你能建議添加一些自定義啓動Bootstrap輪播的其它方式嗎?我期待你的反饋意見。

原文連接:http://www.gbtags.com/gb/share/9588.htm

相關文章
相關標籤/搜索