Bootstrap 轮播加上css3动画,炫酷到底!

对于网站开发者来讲,对展现内容增长一个滑动或者是轮播效果的是很是常见的需求。收费和免费的轮播插件多的是不胜枚举。其中不少提供不少有用的配置选项和动态效果。javascript

不少时候,若是你的项目须要的是一个轻量级的轮播,不须要不少的功能。同时你的项目是采用Bootstrap,(一个最流行的开源前端框架)的话。你能够参考一下bootstrap官方组件。css

这篇文章将展现如何在 Bootstrap轮播上添加有趣的动画效果。同时确保这个js组件自由扩展和快速上手。html

 

介绍Animate.css

为了让我本身写的动画效果值得称赞,我用一个很是有名的开源的CSS3动画库,被形象的称为animate.css。 Dan Eden写的。前端

这是让我能专一于手头的任务,而不是解释CSS3动画的代码。然而,若是你想深刻的了解,你能够在SitePoint上Craig Buckler写的CSS3动画系列进一步学习。java

 

用Animate.css 须要2个步骤:

  1. 在html文档中引入animate.min.css。
  2. 在网页中要加动画的元素上添加animated yourchosenanimation类。

接下来你用Animate.css网站上的看到的关于动画的类名,代替yourchosenanimation。css3

 

引入Bootstrap轮播组件

Bootstrap轮播组件有三个主要的部分。web

  • 轮播指示显示幻灯的页面数量,给用户提供一个视觉线索,并提供能够滑动的导航。
  • 轮播条目,一个叫.carousel-inner的类,包含在外边框的里边。表明每个独立的滑块。每一个图片里边的均可以放置图片。也能够添加标题。还能够在html元素上添加carousel-caption类名。Bootstrap会有自带的样式。咱们能够经过这些元素添加动画。
  • 最后,是轮播控制箭头,功能是可使用户先后滑动。

若是想了解更多Bootstrap轮播组件的详情,能够查看Syed Fazle Rahman的用Bootstrap3建立js轮播效果这篇文章。bootstrap

为了简单的展现demo,就先不加图片了。焦点先放在轮播框架上做为动画。浏览器

 

构建HTML结构

下边是你须要引用到你项目当中的:缓存

  • jQuery
  • Bootstrap’s CSS and JavaScript
  • Animate.css
  • 一个样式表和js文档。

为了加快开发进程,从Bootstrap官网引用了模板和必要的文件。

下边是Bootstrap轮播代码:

  1. <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  2. <!-- Indicators -->
  3. <ol class="carousel-indicators">
  4. <li data-target="#carousel-example-generic" data-slide-to="0" class="active">
  5. </li>
  6. <li data-target="#carousel-example-generic" data-slide-to="1"></li>
  7. <li data-target="#carousel-example-generic" data-slide-to="2"></li>
  8. </ol>
  9. <!-- Wrapper for slides -->
  10. <div class="carousel-inner" role="listbox">
  11.  
  12. <!-- First slide -->
  13. <div class="item active">
  14. <div class="carousel-caption">
  15. <h3 data-animation="animated bounceInLeft">
  16. This is the caption for slide 1
  17. </h3>
  18. <h3 data-animation="animated bounceInRight">
  19. This is the caption for slide 1
  20. </h3>
  21. <button class="btn btn-primary btn-lg"
  22. data-animation="animated zoomInUp">Button</button>
  23. </div>
  24. </div><!-- /.item -->
  25.  
  26. <!-- Second slide -->
  27. <div class="item">
  28. <div class="carousel-caption">
  29. <h3 class="icon-container" data-animation="animated bounceInDown">
  30. <span class="glyphicon glyphicon-heart"></span>
  31. </h3>
  32. <h3 data-animation="animated bounceInUp">
  33. This is the caption for slide 2
  34. </h3>
  35. <button class="btn btn-primary btn-lg"
  36. data-animation="animated zoomInRight">Button</button>
  37. </div>
  38. </div><!-- /.item -->
  39.  
  40. <!-- Third slide -->
  41. <div class="item">
  42. <div class="carousel-caption">
  43. <h3 class="icon-container" data-animation="animated zoomInLeft">
  44. <span class="glyphicon glyphicon-glass"></span>
  45. </h3>
  46. <h3 data-animation="animated flipInX">
  47. This is the caption for slide 3
  48. </h3>
  49. <button class="btn btn-primary btn-lg"
  50. data-animation="animated lightSpeedIn">Button</button>
  51. </div>
  52. </div><!-- /.item -->
  53. </div><!-- /.carousel-inner -->
  54.  
  55. <!-- Controls -->
  56. <a class="left carousel-control" href="#carousel-example-generic"
  57. role="button" data-slide="prev">
  58. <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
  59. <span class="sr-only">Previous</span>
  60. </a>
  61.  
  62. <a class="right carousel-control" href="#carousel-example-generic"
  63. role="button" data-slide="next">
  64. <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
  65. <span class="sr-only">Next</span>
  66. </a>
  67.  
  68. </div><!-- /.carousel -->
 

若是以上代码没有错,你在浏览器打开会看到一个能够运行的轮播,上边的一切不包含一行javascript代码。若是你不添加任何图像,

只是在css文档给.carousel .item这个类块添加min-height值防止轮播塌陷。

在轮播标题内的元素添加一个动画属性data-animation,用这个特别的动画类库做为他们的值。

若是你想从Animate.css库体验其余的动画,用你选择的动画类名代替data-animation属性值。

咱们在javascript代码中用data-animation属性值。

虽然一个简单的自动轮播在一些案例中能够找到,可是对于这个案例咱们有更多的控制。

在这个方向的第一步,从元素中删除data-ride="carousel"值,把data-ride属性值初始化儿不用写任何代码。可是,咱们打算用js代

码控制轮播,所以,这个data-ride属性就没必要要了。

 

给轮播加CSS样式

如今根据本身的喜爱,发挥创造力给轮播标题添加样式。我将要写的样式规则是能顺畅工做的demo。

更具体的说,咱们增长动画延迟属性的控制。定义每一个动画何时开始(注意为了简单演示,省略了浏览器前缀)

  1. .carousel-caption h3:first-child {
  2. animation-delay: 1s;
  3. }
  4.  
  5. .carousel-caption h3:nth-child(2) {
  6. animation-delay: 2s;
  7. }
  8.  
  9. .carousel-caption button {
  10. animation-delay: 3s;
  11. }

上面的代码片断中确保元素动画有序开始。还能够作其余的效果。例如,你能够选择前两个标题同时出现。而后是button按钮。能够本身决定,享受乐趣吧。

 

写jQuery代码:

咱们开始初始化这个轮播,在你的自定义的javascript 文件中添加一下代码:

  1. var $myCarousel = $('#carousel-example-generic');
  2. // Initialize carousel
  3. $myCarousel.carousel();

咱们已经动态的设置了轮播,接下来,咱们来解决这个动画。

为了使第一个幻灯片的标题有动画,当页面在浏览器加载完后脚本得运行。随后的幻灯片在动画下进入到咱们的视野,咱们的代码在slide.bs.carousel 事件上运行。意味着一样的代码运行两次:页面加载一次和slide.bs.carousel 事件一次。

由于咱们喜欢遵循不重复的原则,咱们打算把咱们的代码封装在函数中,并在适当的时候引用。

代码:

  1. function doAnimations(elems) {
  2. var animEndEv = 'webkitAnimationEnd animationend';
  3. elems.each(function () {
  4. var $this = $(this),
  5. $animationType = $this.data('animation');
  6. // Add animate.css classes to
  7. // the elements to be animated
  8. // Remove animate.css classes
  9. // once the animation event has ended
  10. $this.addClass($animationType).one(animEndEv, function () {
  11. $this.removeClass($animationType);
  12. });
  13. });
  14. }
  15. // Select the elements to be animated
  16. // in the first slide on page load
  17. var $firstAnimatingElems = $myCarousel.find('.item:first')
  18. .find('[data-animation ^= "animated"]');
  19. // Apply the animation using our function
  20. doAnimations($firstAnimatingElems);
  21. // Pause the carousel
  22. $myCarousel.carousel('pause');
  23.  
  24. // Attach our doAnimations() function to the
  25. // carousel's slide.bs.carousel event
  26. $myCarousel.on('slide.bs.carousel', function (e) {
  27. // Select the elements to be animated inside the active slide
  28. var $animatingElems = $(e.relatedTarget)
  29. .find("[data-animation ^= 'animated']");
  30. doAnimations($animatingElems);
  31. });

上边的代码 咱们来分析一下。

 

来看doAnimations()函数

这个doAnimations() 函数执行的任务以下。

它开始经过缓存变量中含有的animationend事件名称的字符串。这个事件告诉咱们,你可能已经猜到,当每一个动画结束。咱们须要这个点的信息,由于每一次的动画结束后,咱们将animate.css类移除。若是咱们不作移除,轮播的标题将只有一次动画,也就是,只是在第一次轮播显示特定的幻灯片。

  1. var animEndEv = 'webkitAnimationEnd animationend';

接来下,咱们的函数循环遍历每个咱们想要有动画的元素,并获取data-animation的属性值。想上边所说的,这个值包含咱们想要添加给元素的Animate.css类,以便有动画效果。

  1. elems.each(function () {
  2. var $this = $(this),
  3. $animationType = $this.data('animation');
  4. // etc...
  5. });

最后,这个doAnimations() 函数动态添加animate.css类的每一个要执行动画的元素上,当动画结束的时候,还附加了一个事件监听。动画结束后咱们移除从Animate.css添加的类。这样确保下一个轮播灯片回到当前的区域。(你试着删除这段代码,看看会发生什么)

  1. $this.addClass($animationType).one(animEndEv, function () {
  2. $this.removeClass($animationType);
  3. });

 

第一个标题的动画

当页面在浏览器中加载时,咱们在第一个幻灯片中动画的内容:

  1. var $firstAnimatingElems = $myCarousel.find('.item:first')
  2. .find("[data-animation ^= 'animated']");
  3. doAnimations($firstAnimatingElems);

在这个代码中,咱们找到第一张灯片,咱们但愿经过使用data-animation从动画的标题获取动画属性的值。而后咱们把变量 $firstAnimatingElems 当作参数传给doAnimations()函数,而后执行函数。

 

轮播的中止功能

当第一张灯片内容执行完动画之后,咱们中止这个轮播功能。

  1. $myCarousel.carousel('pause');

这是Bootstrap轮播组件防止不停旋转的特征。不停的旋转,可能会让访客生厌。

在这种状况下,我建议确保轮播不直接循环到下一个灯片直到全部的动画运行完毕。能够经过设置在初始化代码中的“间隔”选项来控制这个:

  1. $myCarousel.carousel({
  2. interval: 4000
  3. });

在我看来,一个无限循环轮播标题跳跃每一次的滑动进入视线不理想。

 

轮播幻灯片标题的动画

为每张幻灯片的动画轮播标题变得可见须要如下描述的步骤。

首先,咱们在slide.bs.carousel上添加一个事件监听器。

当幻灯片实例方法被调用时,该事件当即触发。

  1. $myCarousel.on('slide.bs.carousel', function (e) {
  2. // do stuff...
  3. });

接下来,咱们选择当前的灯片,找到咱们但愿增长动画的元素。下边的代码用了slide.bs.carousel事件的.relatedTarget属性来绑定动画。

  1. var $animatingElems = $(e.relatedTarget).find("[data-animation ^= 'animated']");

最后,咱们调用doAnimations()函数,把$animatingElems当作参数传进去。

  1. doAnimations($animatingElems);
 

总体的demo:

demo

 

结论:

正如大家许多人可能知道,轮播有一些须要开发者考虑的问题。

在这篇文章中,我展现了如何添加一些额外的精力,用几行jQuery和animate.css库用在基本的Bootstrap轮播组件。然而,其余相似的css库。或者css3动画,咱们会作的同样好。

你能建议添加一些自定义启动Bootstrap轮播的其它方式吗?我期待你的反馈意见。

原文连接:http://www.gbtags.com/gb/share/9588.htm

相关文章
相关标签/搜索