Bootstrap輪播加上css3動畫,炫酷到底!
很多時候,如果你的項目需要的是一個輕量級的輪播,不需要很多的功能。同時你的項目是采用Bootstrap,(一個最流行的開源前端框架)的話。你可以參考一下bootstrap官方組件。
介紹Animate.css
為了讓我自己寫的動畫效果值得稱贊,我用一個非常有名的開源的CSS3動畫庫,被形象的稱為animate.css。 Dan Eden寫的。
這是讓我能專注于手頭的任務(wù),而不是解釋CSS3動畫的代碼。
用Animate.css 需要2個步驟:
在html文檔中引入animate.min.css。
在網(wǎng)頁中要加動畫的元素上添加animated yourchosenanimation類。
接下來你用Animate.css網(wǎng)站上的看到的關(guān)于動畫的類名,代替yourchosenanimation。
引入Bootstrap輪播組件
Bootstrap輪播組件有三個主要的部分。
- 輪播指示顯示幻燈的頁面數(shù)量,給用戶提供一個視覺線索,并提供可以滑動的導(dǎo)航。
- 輪播條目,一個叫.carousel-inner的類,包含在外邊框的里邊。代表每一個獨立的滑塊。每個圖片里邊的都可以放置圖片。也可以添加標(biāo)題。還可以在html元素上添加carousel-caption類名。Bootstrap會有自帶的樣式。我們可以通過這些元素添加動畫。
- 最后,是輪播控制箭頭,功能是可以使用戶前后滑動。
如果想了解更多Bootstrap輪播組件的詳情,可以查看Syed Fazle Rahman的用Bootstrap3創(chuàng)建js輪播效果這篇文章。
為了簡單的展示demo,就先不加圖片了。焦點先放在輪播框架上作為動畫。
構(gòu)建HTML結(jié)構(gòu)
下邊是你需要引用到你項目當(dāng)中的:
- jQuery
- Bootstrap's CSS and JavaScript
- Animate.css
- 一個樣式表和js文檔。
為了加快開發(fā)進程,從Bootstrap官網(wǎng)引用了模板和必要的文件。
下邊是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值防止輪播塌陷。
在輪播標(biāo)題內(nèi)的元素添加一個動畫屬性data-animation,用這個特別的動畫類庫作為他們的值。
如果你想從Animate.css庫體驗其他的動畫,用你選擇的動畫類名代替data-animation屬性值。
我們在javascript代碼中用data-animation屬性值。
雖然一個簡單的自動輪播在一些案例中可以找到,但是對于這個案例我們有更多的控制。
在這個方向的第一步,從元素中刪除data-ride="carousel"值,把data-ride屬性值初始化兒不用寫任何代碼。但是,我們打算用js代碼控制輪播,因此,這個data-ride屬性就不必要了。
給輪播加CSS樣式
現(xiàn)在根據(jù)自己的喜好,發(fā)揮創(chuàng)造力給輪播標(biāo)題添加樣式。我將要寫的樣式規(guī)則是能順暢工作的demo。
更具體的說,我們增加動畫延遲屬性的控制。定義每個動畫什么時候開始(注意為了簡單演示,省略了瀏覽器前綴)
.carousel-caption h3:first-child { animation-delay: 1s; } .carousel-caption h3:nth-child(2) { animation-delay: 2s; } .carousel-caption button { animation-delay: 3s; }
上面的代碼片段中確保元素動畫有序開始,還可以做其他的效果。例如,你可以選擇前兩個標(biāo)題同時出現(xiàn),然后是button按鈕,可以自己決定,享受樂趣吧。
寫jQuery代碼:
我們開始初始化這個輪播,在你的自定義的javascript 文件中添加一下代碼:
var $myCarousel = $('#carousel-example-generic'); // Initialize carousel $myCarousel.carousel();
我們已經(jīng)動態(tài)的設(shè)置了輪播,接下來,我們來解決這個動畫。
為了使第一個幻燈片的標(biāo)題有動畫,當(dāng)頁面在瀏覽器加載完后腳本得運行。隨后的幻燈片在動畫下進入到我們的視野,我們的代碼在slide.bs.carousel 事件上運行。意味著同樣的代碼運行兩次:頁面加載一次和slide.bs.carousel 事件一次。
因為我們喜歡遵循不重復(fù)的原則,我們打算把我們的代碼封裝在函數(shù)中,并在適當(dāng)?shù)臅r候引用。
代碼:
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); });
上邊的代碼 我們來分析一下。
1、來看doAnimations()函數(shù)
這個doAnimations() 函數(shù)執(zhí)行的任務(wù)如下。
它開始通過緩存變量中含有的animationend事件名稱的字符串。這個事件告訴我們,你可能已經(jīng)猜到,當(dāng)每個動畫結(jié)束。我們需要這個點的信息,因為每一次的動畫結(jié)束后,我們將animate.css類移除。如果我們不做移除,輪播的標(biāo)題將只有一次動畫,也就是,只是在第一次輪播顯示特定的幻燈片。
var animEndEv = 'webkitAnimationEnd animationend';
接來下,我們的函數(shù)循環(huán)遍歷每一個我們想要有動畫的元素,并獲取data-animation的屬性值。想上邊所說的,這個值包含我們想要添加給元素的Animate.css類,以便有動畫效果。
elems.each(function () { var $this = $(this), $animationType = $this.data('animation'); // etc... });
最后,這個doAnimations() 函數(shù)動態(tài)添加animate.css類的每個要執(zhí)行動畫的元素上,當(dāng)動畫結(jié)束的時候,還附加了一個事件監(jiān)聽。動畫結(jié)束后我們移除從Animate.css添加的類。這樣確保下一個輪播燈片回到當(dāng)前的區(qū)域。(你試著刪除這段代碼,看看會發(fā)生什么)
$this.addClass($animationType).one(animEndEv, function () { $this.removeClass($animationType); });
2、第一個標(biāo)題的動畫
當(dāng)頁面在瀏覽器中加載時,我們在第一個幻燈片中動畫的內(nèi)容:
var $firstAnimatingElems = $myCarousel.find('.item:first') .find("[data-animation ^= 'animated']"); doAnimations($firstAnimatingElems);
在這個代碼中,我們找到第一張燈片,我們希望通過使用data-animation從動畫的標(biāo)題獲取動畫屬性的值。然后我們把變量 $firstAnimatingElems 當(dāng)做參數(shù)傳給doAnimations()函數(shù),然后執(zhí)行函數(shù)。
3、輪播的停止功能
當(dāng)?shù)谝粡垷羝瑑?nèi)容執(zhí)行完動畫以后,我們停止這個輪播功能。
$myCarousel.carousel('pause');
這是Bootstrap輪播組件防止不停旋轉(zhuǎn)的特征。不停的旋轉(zhuǎn),可能會讓訪客生厭。
在這種情況下,我建議確保輪播不直接循環(huán)到下一個燈片直到所有的動畫運行完畢??梢酝ㄟ^設(shè)置在初始化代碼中的“間隔”選項來控制這個:
$myCarousel.carousel({ interval: 4000 });
在我看來,一個無限循環(huán)輪播標(biāo)題跳躍每一次的滑動進入視線不理想。
4、輪播幻燈片標(biāo)題的動畫
為每張幻燈片的動畫輪播標(biāo)題變得可見需要以下描述的步驟。
首先,我們在slide.bs.carousel上添加一個事件監(jiān)聽器。
當(dāng)幻燈片實例方法被調(diào)用時,該事件立即觸發(fā)。
$myCarousel.on('slide.bs.carousel', function (e) { // do stuff... });
接下來,我們選擇當(dāng)前的燈片,找到我們希望增加動畫的元素。下邊的代碼用了slide.bs.carousel事件的.relatedTarget屬性來綁定動畫。
var $animatingElems = $(e.relatedTarget).find("[data-animation ^= 'animated']");
最后,我們調(diào)用doAnimations()函數(shù),把$animatingElems當(dāng)做參數(shù)傳進去。
doAnimations($animatingElems);
正如你們許多人可能知道,輪播有一些需要開發(fā)者考慮的問題。
在這篇文章中,展示了如何添加一些額外的精力,用幾行jQuery和animate.css庫用在基本的Bootstrap輪播組件。然而,其他類似的css庫,或者css3動畫,我們會做的一樣好,希望這篇文章可以給大家?guī)砀嗟膯l(fā),打開大家的學(xué)習(xí)思路。
如果大家還想深入學(xué)習(xí),可以點擊這里進行學(xué)習(xí),再為大家附兩個精彩的專題:Bootstrap學(xué)習(xí)教程 Bootstrap實戰(zhàn)教程
精彩專題分享:jQuery圖片輪播 JavaScript圖片輪播 Bootstrap圖片輪播
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實現(xiàn)可拖拽的拖動層Div實例
這篇文章主要介紹了JavaScript實現(xiàn)可拖拽的拖動層Div的方法,拖拽頁面中的div塊可實現(xiàn)div塊按照拖動軌跡移動的效果,涉及javascript鼠標(biāo)事件、頁面元素樣式結(jié)合事件函數(shù)動態(tài)操作的相關(guān)技巧,需要的朋友可以參考下2015-08-08JS中new?Blob()詳解及blob轉(zhuǎn)file示例
這篇文章主要給大家介紹了關(guān)于JS中new?Blob()詳解及blob轉(zhuǎn)file的相關(guān)資料,Blob?Blob(Binary?Large?Object)表示二進制類型的大對象,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11JavaScript常用函數(shù)工具集:lao-utils
現(xiàn)在Javascript庫海量,流行的也多,比如jQuery,YUI等,雖然功能強大,但也是不萬能的,功能不可能涉及方方面面,這里給大家分享一個的JS庫是對這些的補充,很多也比較實用,把應(yīng)用到項目中中去也比較方面,這也是對工作的一些積累,也加深對知識的理解。2016-03-03微信小程序 騰訊地圖SDK 獲取當(dāng)前地址實現(xiàn)解析
這篇文章主要介紹了微信小程序 騰訊地圖SDK 獲取當(dāng)前地址實現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08淺談js圖片前端預(yù)覽之filereader和window.URL.createObjectURL
下面小編就為大家?guī)硪黄獪\談js圖片前端預(yù)覽之filereader和window.URL.createObjectURL。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06