對於網站開發者來講,對展現內容增長一個滑動或者是輪播效果的是很是常見的需求。收費和免費的輪播插件多的是不勝枚舉。其中不少提供不少有用的配置選項和動態效果。javascript
不少時候,若是你的項目須要的是一個輕量級的輪播,不須要不少的功能。同時你的項目是採用Bootstrap,(一個最流行的開源前端框架)的話。你能夠參考一下bootstrap官方組件。css
這篇文章將展現如何在 Bootstrap輪播上添加有趣的動畫效果。同時確保這個js組件自由擴展和快速上手。html
介紹Animate.css
爲了讓我本身寫的動畫效果值得稱讚,我用一個很是有名的開源的CSS3動畫庫,被形象的稱爲animate.css。 Dan Eden寫的。前端
這是讓我能專一於手頭的任務,而不是解釋CSS3動畫的代碼。然而,若是你想深刻的瞭解,你能夠在SitePoint上Craig Buckler寫的CSS3動畫系列進一步學習。java
用Animate.css 須要2個步驟:
- 在html文檔中引入animate.min.css。
- 在網頁中要加動畫的元素上添加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輪播代碼:
- <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
- <!-- Indicators -->
- <ol class="carousel-indicators">
- <li data-target="#carousel-example-generic" data-slide-to="0" class="active">
- </li>
- <li data-target="#carousel-example-generic" data-slide-to="1"></li>
- <li data-target="#carousel-example-generic" data-slide-to="2"></li>
- </ol>
- <!-- Wrapper for slides -->
- <div class="carousel-inner" role="listbox">
-
- <!-- First slide -->
- <div class="item active">
- <div class="carousel-caption">
- <h3 data-animation="animated bounceInLeft">
- This is the caption for slide 1
- </h3>
- <h3 data-animation="animated bounceInRight">
- This is the caption for slide 1
- </h3>
- <button class="btn btn-primary btn-lg"
- data-animation="animated zoomInUp">Button</button>
- </div>
- </div><!-- /.item -->
-
- <!-- Second slide -->
- <div class="item">
- <div class="carousel-caption">
- <h3 class="icon-container" data-animation="animated bounceInDown">
- <span class="glyphicon glyphicon-heart"></span>
- </h3>
- <h3 data-animation="animated bounceInUp">
- This is the caption for slide 2
- </h3>
- <button class="btn btn-primary btn-lg"
- data-animation="animated zoomInRight">Button</button>
- </div>
- </div><!-- /.item -->
-
- <!-- Third slide -->
- <div class="item">
- <div class="carousel-caption">
- <h3 class="icon-container" data-animation="animated zoomInLeft">
- <span class="glyphicon glyphicon-glass"></span>
- </h3>
- <h3 data-animation="animated flipInX">
- This is the caption for slide 3
- </h3>
- <button class="btn btn-primary btn-lg"
- data-animation="animated lightSpeedIn">Button</button>
- </div>
- </div><!-- /.item -->
- </div><!-- /.carousel-inner -->
-
- <!-- Controls -->
- <a class="left carousel-control" href="#carousel-example-generic"
- role="button" data-slide="prev">
- <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
- <span class="sr-only">Previous</span>
- </a>
-
- <a class="right carousel-control" href="#carousel-example-generic"
- role="button" data-slide="next">
- <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
- <span class="sr-only">Next</span>
- </a>
-
- </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。
更具體的說,咱們增長動畫延遲屬性的控制。定義每一個動畫何時開始(注意爲了簡單演示,省略了瀏覽器前綴)
- .carousel-caption h3:first-child {
- animation-delay: 1s;
- }
-
- .carousel-caption h3:nth-child(2) {
- animation-delay: 2s;
- }
-
- .carousel-caption button {
- animation-delay: 3s;
- }
上面的代碼片斷中確保元素動畫有序開始。還能夠作其餘的效果。例如,你能夠選擇前兩個標題同時出現。而後是button按鈕。能夠本身決定,享受樂趣吧。
寫jQuery代碼:
咱們開始初始化這個輪播,在你的自定義的javascript 文件中添加一下代碼:
- var $myCarousel = $('#carousel-example-generic');
- // Initialize carousel
- $myCarousel.carousel();
咱們已經動態的設置了輪播,接下來,咱們來解決這個動畫。
爲了使第一個幻燈片的標題有動畫,當頁面在瀏覽器加載完後腳本得運行。隨後的幻燈片在動畫下進入到咱們的視野,咱們的代碼在slide.bs.carousel 事件上運行。意味着一樣的代碼運行兩次:頁面加載一次和slide.bs.carousel 事件一次。
由於咱們喜歡遵循不重複的原則,咱們打算把咱們的代碼封裝在函數中,並在適當的時候引用。
代碼:
- function doAnimations(elems) {
- var animEndEv = 'webkitAnimationEnd animationend';
- elems.each(function () {
- var $this = $(this),
- $animationType = $this.data('animation');
- // Add animate.css classes to
- // the elements to be animated
- // Remove animate.css classes
- // once the animation event has ended
- $this.addClass($animationType).one(animEndEv, function () {
- $this.removeClass($animationType);
- });
- });
- }
- // Select the elements to be animated
- // in the first slide on page load
- var $firstAnimatingElems = $myCarousel.find('.item:first')
- .find('[data-animation ^= "animated"]');
- // Apply the animation using our function
- doAnimations($firstAnimatingElems);
- // Pause the carousel
- $myCarousel.carousel('pause');
-
- // Attach our doAnimations() function to the
- // carousel's slide.bs.carousel event
- $myCarousel.on('slide.bs.carousel', function (e) {
- // Select the elements to be animated inside the active slide
- var $animatingElems = $(e.relatedTarget)
- .find("[data-animation ^= 'animated']");
- doAnimations($animatingElems);
- });
上邊的代碼 咱們來分析一下。
來看doAnimations()函數
這個doAnimations() 函數執行的任務以下。
它開始經過緩存變量中含有的animationend事件名稱的字符串。這個事件告訴咱們,你可能已經猜到,當每一個動畫結束。咱們須要這個點的信息,由於每一次的動畫結束後,咱們將animate.css類移除。若是咱們不作移除,輪播的標題將只有一次動畫,也就是,只是在第一次輪播顯示特定的幻燈片。
- var animEndEv = 'webkitAnimationEnd animationend';
接來下,咱們的函數循環遍歷每個咱們想要有動畫的元素,並獲取data-animation的屬性值。想上邊所說的,這個值包含咱們想要添加給元素的Animate.css類,以便有動畫效果。
- elems.each(function () {
- var $this = $(this),
- $animationType = $this.data('animation');
- // etc...
- });
最後,這個doAnimations() 函數動態添加animate.css類的每一個要執行動畫的元素上,當動畫結束的時候,還附加了一個事件監聽。動畫結束後咱們移除從Animate.css添加的類。這樣確保下一個輪播燈片回到當前的區域。(你試着刪除這段代碼,看看會發生什麼)
- $this.addClass($animationType).one(animEndEv, function () {
- $this.removeClass($animationType);
- });
第一個標題的動畫
當頁面在瀏覽器中加載時,咱們在第一個幻燈片中動畫的內容:
- var $firstAnimatingElems = $myCarousel.find('.item:first')
- .find("[data-animation ^= 'animated']");
- doAnimations($firstAnimatingElems);
在這個代碼中,咱們找到第一張燈片,咱們但願經過使用data-animation從動畫的標題獲取動畫屬性的值。而後咱們把變量 $firstAnimatingElems 當作參數傳給doAnimations()函數,而後執行函數。
輪播的中止功能
當第一張燈片內容執行完動畫之後,咱們中止這個輪播功能。
- $myCarousel.carousel('pause');
這是Bootstrap輪播組件防止不停旋轉的特徵。不停的旋轉,可能會讓訪客生厭。
在這種狀況下,我建議確保輪播不直接循環到下一個燈片直到全部的動畫運行完畢。能夠經過設置在初始化代碼中的「間隔」選項來控制這個:
- $myCarousel.carousel({
- interval: 4000
- });
在我看來,一個無限循環輪播標題跳躍每一次的滑動進入視線不理想。
輪播幻燈片標題的動畫
爲每張幻燈片的動畫輪播標題變得可見須要如下描述的步驟。
首先,咱們在slide.bs.carousel上添加一個事件監聽器。
當幻燈片實例方法被調用時,該事件當即觸發。
- $myCarousel.on('slide.bs.carousel', function (e) {
- // do stuff...
- });
接下來,咱們選擇當前的燈片,找到咱們但願增長動畫的元素。下邊的代碼用了slide.bs.carousel事件的.relatedTarget屬性來綁定動畫。
- var $animatingElems = $(e.relatedTarget).find("[data-animation ^= 'animated']");
最後,咱們調用doAnimations()函數,把$animatingElems當作參數傳進去。
- doAnimations($animatingElems);
總體的demo:
demo
結論:
正如大家許多人可能知道,輪播有一些須要開發者考慮的問題。
在這篇文章中,我展現瞭如何添加一些額外的精力,用幾行jQuery和animate.css庫用在基本的Bootstrap輪播組件。然而,其餘相似的css庫。或者css3動畫,咱們會作的同樣好。
你能建議添加一些自定義啓動Bootstrap輪播的其它方式嗎?我期待你的反饋意見。
原文連接:http://www.gbtags.com/gb/share/9588.htm