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