非??犰诺腂ootstrap圖片輪播動(dòng)畫(huà)
對(duì)于網(wǎng)站開(kāi)發(fā)者來(lái)說(shuō),對(duì)展示內(nèi)容增加一個(gè)滑動(dòng)或者是輪播效果的是非常常見(jiàn)的需求。收費(fèi)和免費(fèi)的輪播插件多的是不勝枚舉。其中很 多提供很多有用的配置選項(xiàng)和動(dòng)態(tài)效果。
很多時(shí)候,如果你的項(xiàng)目需要的是一個(gè)輕量級(jí)的輪播,不需要很多的功能。同時(shí)你的項(xiàng)目是采用Bootstrap,(一個(gè)最流行的開(kāi)源前端 框架)的話(huà)。你可以參考一下bootstrap官方組件。
這篇文章將展示如何在 Bootstrap輪播上添加有趣的動(dòng)畫(huà)效果。同時(shí)確保這個(gè)js組件自由擴(kuò)展和快速上手。
介紹Animate.css
為了讓我自己寫(xiě)的動(dòng)畫(huà)效果值得稱(chēng)贊,我用一個(gè)非常有名的開(kāi)源的CSS3動(dòng)畫(huà)庫(kù),被形象的稱(chēng)為animate.css。 Dan Eden寫(xiě)的。
這是讓我能專(zhuān)注于手頭的任務(wù),而不是解釋CSS3動(dòng)畫(huà)的代碼。
用Animate.css 需要2個(gè)步驟:
1、在html文檔中引入animate.min.css。
2、在網(wǎng)頁(yè)中要加動(dòng)畫(huà)的元素上添加animated yourchosenanimation類(lèi)。
接下來(lái)你用Animate.css網(wǎng)站上的看到的關(guān)于動(dòng)畫(huà)的類(lèi)名,代替yourchosenanimation。
引入Bootstrap輪播組件
Bootstrap輪播組件有三個(gè)主要的部分。
---輪播指示顯示幻燈的頁(yè)面數(shù)量,給用戶(hù)提供一個(gè)視覺(jué)線(xiàn)索,并提供可以滑動(dòng)的導(dǎo)航。
---輪播條目,一個(gè)叫.carousel-inner的類(lèi),包含在外邊框的里邊。代表每一個(gè)獨(dú)立的滑塊。每個(gè)圖片里邊的都可以放置圖片。也可以 添加標(biāo)題。還可以在html元素上添加carousel-caption類(lèi)名。Bootstrap會(huì)有自帶的樣式。我們可以通過(guò)這些元素添加動(dòng)畫(huà)。
---最后,是輪播控制箭頭,功能是可以使用戶(hù)前后滑動(dòng)。
為了簡(jiǎn)單的展示demo,就先不加圖片了。焦點(diǎn)先放在輪播框架上作為動(dòng)畫(huà)。

構(gòu)建HTML結(jié)構(gòu)
下邊是你需要引用到你項(xiàng)目當(dāng)中的:
jQuery
Bootstrap's CSS and JavaScript
Animate.css
一個(gè) 樣式表和js文檔。
為了加快開(kāi)發(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 -->
如果以上代碼沒(méi)有錯(cuò),你在瀏覽器打開(kāi)會(huì)看到一個(gè)可以運(yùn)行的輪播,上邊的一切不包含一行javascript代碼。如果你不添加任何圖像,
只是在css文檔給.carousel .item這個(gè)類(lèi)塊添加min-height值防止輪播塌陷。
在輪播標(biāo)題內(nèi)的元素添加一個(gè)動(dòng)畫(huà)屬性data-animation,用這個(gè)特別的動(dòng)畫(huà)類(lèi)庫(kù)作為他們的值。
如果你想從Animate.css庫(kù)體驗(yàn)其他的動(dòng)畫(huà),用你選擇的動(dòng)畫(huà)類(lèi)名代替data-animation屬性值。
我們?cè)趈avascript代碼中用data-animation屬性值。
雖然一個(gè)簡(jiǎn)單的自動(dòng)輪播在一些案例中可以找到,但是對(duì)于這個(gè)案例我們有更多的控制。
在這個(gè)方向的第一步,從元素中刪除data-ride="carousel"值,把data-ride屬性值初始化兒不用寫(xiě)任何代碼。但是,我們打算用js代碼控制輪播,因此,這個(gè)data-ride屬性就不必要了。
給輪播加CSS樣式
現(xiàn)在根據(jù)自己的喜好,發(fā)揮創(chuàng)造力給輪播標(biāo)題添加樣式。我將要寫(xiě)的樣式規(guī)則是能順暢工作的demo。
更具體的說(shuō),我們?cè)黾觿?dòng)畫(huà)延遲屬性的控制。定義每個(gè)動(dòng)畫(huà)什么時(shí)候開(kāi)始(注意為了簡(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)畫(huà)有序開(kāi)始。還可以做其他的效果。例如,你可以選擇前兩個(gè)標(biāo)題同時(shí)出現(xiàn)。然后是button按鈕??梢宰?己決定,享受樂(lè)趣吧。
寫(xiě)jQuery代碼:
我們開(kāi)始初始化這個(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)畫(huà)。
為了使第一個(gè)幻燈片的標(biāo)題有動(dòng)畫(huà),當(dāng)頁(yè)面在瀏覽器加載完后腳本得運(yùn)行。隨后的幻燈片在動(dòng)畫(huà)下進(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)分析一下。
來(lái)看doAnimations()函數(shù)
這個(gè)doAnimations() 函數(shù)執(zhí)行的任務(wù)如下。
它開(kāi)始通過(guò)緩存變量中含有的animationend事件名稱(chēng)的字符串。這個(gè)事件告訴我們,你可能已經(jīng)猜到,當(dāng)每個(gè)動(dòng)畫(huà)結(jié)束。我們需要這個(gè) 點(diǎn)的信息,因?yàn)槊恳淮蔚膭?dòng)畫(huà)結(jié)束后,我們將animate.css類(lèi)移除。如果我們不做移除,輪播的標(biāo)題將只有一次動(dòng)畫(huà),也就是,只是在 第一次輪播顯示特定的幻燈片。
var animEndEv = 'webkitAnimationEnd animationend';
接來(lái)下,我們的函數(shù)循環(huán)遍歷每一個(gè)我們想要有動(dòng)畫(huà)的元素,并獲取data-animation的屬性值。想上邊所說(shuō)的,這個(gè)值包含我們想要添 加給元素的Animate.css類(lèi),以便有動(dòng)畫(huà)效果。
elems.each(function () {
var $this = $(this),
$animationType = $this.data('animation');
// etc...
});
最后,這個(gè)doAnimations() 函數(shù)動(dòng)態(tài)添加animate.css類(lèi)的每個(gè)要執(zhí)行動(dòng)畫(huà)的元素上,當(dāng)動(dòng)畫(huà)結(jié)束的時(shí)候,還附加了一個(gè)事件監(jiān)聽(tīng)。動(dòng) 畫(huà)結(jié)束后我們移除從Animate.css添加的類(lèi)。這樣確保下一個(gè)輪播燈片回到當(dāng)前的區(qū)域。(你試著刪除這段代碼,看看會(huì)發(fā)生什么)
$this.addClass($animationType).one(animEndEv, function () {
$this.removeClass($animationType);
});
第一個(gè)標(biāo)題的動(dòng)畫(huà)
當(dāng)頁(yè)面在瀏覽器中加載時(shí),我們?cè)诘谝粋€(gè)幻燈片中動(dòng)畫(huà)的內(nèi)容:
var $firstAnimatingElems = $myCarousel.find('.item:first')
.find("[data-animation ^= 'animated']");
doAnimations($firstAnimatingElems);
在這個(gè)代碼中,我們找到第一張燈片,我們希望通過(guò)使用data-animation從動(dòng)畫(huà)的標(biāo)題獲取動(dòng)畫(huà)屬性的值。然后我們把變量 $firstAnimatingElems 當(dāng)做參數(shù)傳給doAnimations()函數(shù),然后執(zhí)行函數(shù)。
輪播的停止功能
當(dāng)?shù)谝粡垷羝瑑?nèi)容執(zhí)行完動(dòng)畫(huà)以后,我們停止這個(gè)輪播功能。
$myCarousel.carousel('pause');
這是Bootstrap輪播組件防止不停旋轉(zhuǎn)的特征。不停的旋轉(zhuǎn),可能會(huì)讓訪客生厭。
在這種情況下,我建議確保輪播不直接循環(huán)到下一個(gè)燈片直到所有的動(dòng)畫(huà)運(yùn)行完畢??梢酝ㄟ^(guò)設(shè)置在初始化代碼中的“間隔”選項(xiàng)來(lái)控 制這個(gè):
$myCarousel.carousel({
interval: 4000
});
在我看來(lái),一個(gè)無(wú)限循環(huán)輪播標(biāo)題跳躍每一次的滑動(dòng)進(jìn)入視線(xiàn)不理想。
輪播幻燈片標(biāo)題的動(dòng)畫(huà)
為每張幻燈片的動(dòng)畫(huà)輪播標(biāo)題變得可見(jiàn)需要以下描述的步驟。
首先,我們?cè)趕lide.bs.carousel上添加一個(gè)事件監(jiān)聽(tīng)器。
當(dāng)幻燈片實(shí)例方法被調(diào)用時(shí),該事件立即觸發(fā)。
$myCarousel.on('slide.bs.carousel', function (e) {
// do stuff...
});
接下來(lái),我們選擇當(dāng)前的燈片,找到我們希望增加動(dòng)畫(huà)的元素。下邊的代碼用了slide.bs.carousel事件的.relatedTarget屬性來(lái)綁定 動(dòng)畫(huà)。
var $animatingElems = $(e.relatedTarget).find("[data-animation ^= 'animated']");
最后,我們調(diào)用doAnimations()函數(shù),把$animatingElems當(dāng)做參數(shù)傳進(jìn)去。
doAnimations($animatingElems);
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊這里進(jìn)行學(xué)習(xí),再為大家附兩個(gè)精彩的專(zhuān)題:Bootstrap學(xué)習(xí)教程 Bootstrap實(shí)戰(zhàn)教程
精彩專(zhuān)題分享:jQuery圖片輪播 JavaScript圖片輪播 Bootstrap圖片輪播
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
學(xué)習(xí)JavaScript圖片預(yù)加載模塊
這篇文章主要介紹了js實(shí)現(xiàn)圖片預(yù)加載的方法,內(nèi)容很詳細(xì),帶領(lǐng)大家全面認(rèn)識(shí)js圖片預(yù)加載模塊,感興趣的小伙伴們可以參考一下2016-11-11
Bootstrapvalidator校驗(yàn)、校驗(yàn)清除重置的實(shí)現(xiàn)代碼(推薦)
這篇文章給大家介紹了bootstrapvalidator校驗(yàn)、校驗(yàn)清除重置的實(shí)現(xiàn)代碼,在代碼中需要我們引入css與js文件,大家可以參考下文的代碼2016-09-09
windows系統(tǒng)下簡(jiǎn)單nodejs安裝及環(huán)境配置
相信對(duì)于很多關(guān)注javascript發(fā)展的同學(xué)來(lái)說(shuō),nodejs已經(jīng)不是一個(gè)陌生的詞眼,這里不想談太多的nodejs的相關(guān)信息。只說(shuō)一下,windows系統(tǒng)下簡(jiǎn)單nodejs環(huán)境配置2013-01-01
JavaScript實(shí)現(xiàn)無(wú)限輪播效果
這篇文章主要介為大家詳細(xì)紹了JavaScript實(shí)現(xiàn)無(wú)限輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11

