
如果想了解更多Bootstrap轮播组件的详情,可以查看Syed Fazle Rahman的用Bootstrap3创建js轮播效果这篇文章。
为了简单的展示demo,就先不加图片了。焦点先放在轮播框架上作为动画。
构建HTML结构
下边是你需要引用到你项目当中的:
<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值防止轮播塌陷。
.carousel-caption h3:first-child { animation-delay: 1s;} .carousel-caption h3:nth-child(2) { animation-delay: 2s;} .carousel-caption button { animation-delay: 3s;}上面的代码片段中确保元素动画有序开始,还可以做其他的效果。例如,你可以选择前两个标题同时出现,然后是button按钮,可以自己决定,享受乐趣吧。var $myCarousel = $("#carousel-example-generic");// Initialize carousel$myCarousel.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 loadvar $firstAnimatingElems = $myCarousel.find(".item:first") .find("[data-animation ^= "animated"]");// Apply the animation using our functiondoAnimations($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 slidevar $animatingElems = $(e.relatedTarget).find("[data-animation ^= "animated"]"); doAnimations($animatingElems);});上边的代码 我们来分析一下。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);});2、第一个标题的动画var $firstAnimatingElems = $myCarousel.find(".item:first") .find("[data-animation ^= "animated"]"); doAnimations($firstAnimatingElems);在这个代码中,我们找到第一张灯片,我们希望通过使用data-animation从动画的标题获取动画属性的值。然后我们把变量 $firstAnimatingElems 当做参数传给doAnimations()函数,然后执行函数。
$myCarousel.carousel("pause");这是Bootstrap轮播组件防止不停旋转的特征。不停的旋转,可能会让访客生厌。
$myCarousel.carousel({ interval: 4000});在我看来,一个无限循环轮播标题跳跃每一次的滑动进入视线不理想。$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);正如你们许多人可能知道,轮播有一些需要开发者考虑的问题。