對(duì)于網(wǎng)站開發(fā)者來說,對(duì)展示內(nèi)容增加一個(gè)滑動(dòng)或者是輪播效果的是非常常見的需求。收費(fèi)和免費(fèi)的輪播插件多的是不勝枚舉。其中很 多提供很多有用的配置選項(xiàng)和動(dòng)態(tài)效果。
很多時(shí)候,如果你的項(xiàng)目需要的是一個(gè)輕量級(jí)的輪播,不需要很多的功能。同時(shí)你的項(xiàng)目是采用Bootstrap,(一個(gè)最流行的開源前端 框架)的話。你可以參考一下bootstrap官方組件。
這篇文章將展示如何在 Bootstrap輪播上添加有趣的動(dòng)畫效果。同時(shí)確保這個(gè)js組件自由擴(kuò)展和快速上手。
介紹Animate.css
為了讓我自己寫的動(dòng)畫效果值得稱贊,我用一個(gè)非常有名的開源的CSS3動(dòng)畫庫,被形象的稱為animate.css。 Dan Eden寫的。
這是讓我能專注于手頭的任務(wù),而不是解釋CSS3動(dòng)畫的代碼。
用Animate.css 需要2個(gè)步驟:
1、在html文檔中引入animate.min.css。
2、在網(wǎng)頁中要加動(dòng)畫的元素上添加animated yourchosenanimation類。
接下來你用Animate.css網(wǎng)站上的看到的關(guān)于動(dòng)畫的類名,代替yourchosenanimation。
引入Bootstrap輪播組件
Bootstrap輪播組件有三個(gè)主要的部分。
---輪播指示顯示幻燈的頁面數(shù)量,給用戶提供一個(gè)視覺線索,并提供可以滑動(dòng)的導(dǎo)航。
---輪播條目,一個(gè)叫.carousel-inner的類,包含在外邊框的里邊。代表每一個(gè)獨(dú)立的滑塊。每個(gè)圖片里邊的都可以放置圖片。也可以 添加標(biāo)題。還可以在html元素上添加carousel-caption類名。Bootstrap會(huì)有自帶的樣式。我們可以通過這些元素添加動(dòng)畫。
---最后,是輪播控制箭頭,功能是可以使用戶前后滑動(dòng)。
為了簡單的展示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)畫類庫作為他們的值。
如果你想從Animate.css庫體驗(yàn)其他的動(dòng)畫,用你選擇的動(dòng)畫類名代替data-animation屬性值。
我們?cè)趈avascript代碼中用data-animation屬性值。
雖然一個(gè)簡單的自動(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í)候開始(注意為了簡單演示,省略了瀏覽器前綴)
.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è)置了輪播,接下來,我們來解決這個(gè)動(dòng)畫。
為了使第一個(gè)幻燈片的標(biāo)題有動(dòng)畫,當(dāng)頁面在瀏覽器加載完后腳本得運(yùn)行。隨后的幻燈片在動(dòng)畫下進(jìn)入到我們的視野,我們的代碼在 slide.bs.carousel 事件上運(yùn)行。意味著同樣的代碼運(yùn)行兩次:頁面加載一次和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);
});
上邊的代碼 我們來分析一下。
來看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';
接來下,我們的函數(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);
});
第一個(gè)標(biāo)題的動(dòng)畫
當(dāng)頁面在瀏覽器中加載時(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ù)。
輪播的停止功能
當(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)來控 制這個(gè):
$myCarousel.carousel({
interval: 4000
});
在我看來,一個(gè)無限循環(huán)輪播標(biāo)題跳躍每一次的滑動(dòng)進(jìn)入視線不理想。
輪播幻燈片標(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...
});
接下來,我們選擇當(dāng)前的燈片,找到我們希望增加動(dòng)畫的元素。下邊的代碼用了slide.bs.carousel事件的.relatedTarget屬性來綁定 動(dòng)畫。
var $animatingElems = $(e.relatedTarget).find("[data-animation ^= 'animated']");
最后,我們調(diào)用doAnimations()函數(shù),把$animatingElems當(dāng)做參數(shù)傳進(jìn)去。
doAnimations($animatingElems);
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助