使用jQuery timelinr和animate.css创建超酷的CSS动画时间轴特效

在过去我们的文章中,我们介绍过很多不错的时间轴插件,使用这些超棒的插件可以帮助你创建动感漂亮的时间轴展示,其中比较不错的插件如下:

使用以上jQuery插件或者类库都可以创建漂亮的时间轴timline特效。

由于在我们的gbtags.com社区开发中,我们需要创建一个简单实用的用户时间轴应用,这里我们选择使用Timelinr来开发,并且为了使得动画过程更加丰富和有趣我们同时使用Animate.css来创建各种不同的CSS动画特效。

需要使用到的第三方插件和CSS类库如下:

Timelinr是一个时间轴的jQuery插件实现,你可以方便的使用它来生成一个动态的时间轴特效,提供了垂直和水平显示方式,并且支持自动播放。

Animate.css是由Dan Eden开发的一套超棒的CSS动画类库,帮助你使用纯CSS来实现各种不同的动画特效。前面我们曾经专题介绍过Animate.css,如果你不了解,请阅读这篇文章:超棒的跨浏览器纯CSS动画实现 - Animate.css

fixie.js在我们以前的文章中介绍过,是一个自动帮助你填充内容的迷你类库,如果你厌倦了拷贝一堆内容的话,可以使用它来自动生成内容,个人非常喜欢,这里我将使用它来生成图片和文字内容。

这里我们使用cufon在生成更加个性化的年份,cufon.js会将制定的文字转化为画布图片。

Javascript代码

因为jQuery timelinr是一个插件,为了更好的封装它,我们这里直接修改插件内容,缺省的动画效果比较简单,只是在每个页面以放大的方式来展示图片,我们希望能够添加更多特效,这里通过添加animate.css中定义的动画class来实现。

使用animate非常简单,你只需要使用jQuery的addClass方法调用对应的类,即可实现CSS动画效果,如下:

  1. $(‘somediv').addClass('animated shake').delay(1000).queue(function(next){
  2. $(this).removeClass('animated shake');
  3. next();
  4. });

注意以上代码中,我们使用delay方法来延迟1秒来让动画完成,然后再将添加的class删除,以便下次再次调用addClass生成动画效果

在插件中找到相关代码,如下:

  1. $(settings.issuesDiv+' li').animate({'opacity':settings.issuesTransparency},{queue:false, duration:settings.issuesSpeed});
  2. $(settings.issuesDiv+' li.'+settings.issuesSelectedClass).removeClass(settings.issuesSelectedClass).next().fadeTo(settings.issuesTransparencySpeed, 1).addClass(settings.issuesSelectedClass);

注释后,换成我们需要执行的动画:

  1. $(settings.issuesDiv+' li').addClass('animated ' + cssAnimation).delay(1000).queue(function(next){
  2. $(this).removeClass('animated ' + cssAnimation);
  3. next();
  4. });

另外, 我们使用cufon来将文字生成图片,主要需要在class变化的时候,重新调用cufon,如下:

  1. $(settings.datesDiv+' a.'+settings.datesSelectedClass).removeClass(settings.datesSelectedClass).parent().next().children().addClass(settings.datesSelectedClass).delay(500).queue(
  2. function(next){
  3. next();
  4. Cufon.refresh();
  5. });

这里我们添加一个选项cssAnimation,缺省值为“lightSpeedIn”,这样方便我们自己定义动画类型。

  1. settings = jQuery.extend({
  2. orientation: 'horizontal', // value: horizontal | vertical, default to horizontal
  3. containerDiv: '#timeline', // value: any HTML tag or #id, default to #timeline
  4. datesDiv: '#dates', // value: any HTML tag or #id, default to #dates
  5. datesSelectedClass: 'selected', // value: any class, default to selected
  6. datesSpeed: 'normal', // value: integer between 100 and 1000 (recommended) or 'slow', 'normal' or 'fast'; default to normal
  7. issuesDiv: '#issues', // value: any HTML tag or #id, default to #issues
  8. issuesSelectedClass: 'selected', // value: any class, default to selected
  9. issuesSpeed: 'fast', // value: integer between 100 and 1000 (recommended) or 'slow', 'normal' or 'fast'; default to fast
  10. issuesTransparency: 0.2, // value: integer between 0 and 1 (recommended), default to 0.2
  11. issuesTransparencySpeed: 500, // value: integer between 100 and 1000 (recommended), default to 500 (normal)
  12. prevButton: '#prev', // value: any HTML tag or #id, default to #prev
  13. nextButton: '#next', // value: any HTML tag or #id, default to #next
  14. arrowKeys: 'false', // value: true | false, default to false
  15. startAt: 1, // value: integer, default to 1 (first)
  16. autoPlay: 'false', // value: true | false, default to false
  17. autoPlayDirection: 'forward', // value: forward | backward, default to forward
  18. autoPlayPause: 2000, // value: integer (1000 = 1 seg), default to 2000 (2segs)
  19. cssAnimation: 'lightSpeedIn'
  20. }, options);

javascript调用如下:

  1. $(function(){
  2. Cufon.replace('#timeline a, #timeline h1').CSS.ready(function() {
  3. $().timelinr({autoPlay:'true', autoPlayPause:'3000', cssAnimation:'tada'});
  4. });
  5. });

以上代码可以看到,我们调用cufon将所有需要生成美化字体的元素都替换掉,然后调用timeliner插件,这里我们自定义动画类型为:tada,如果你需要生成其它效果,请自己修改类型。

HTML代码

HTML中我们定义了年份和每一个时间轴项目的内容,包括,文字和图片,这里代码很简单,只包含了一个框架,我们使用fixie.js来自动生成具体内容:

  1. <div >
  2. <ul >
  3. <li><a href="#2001">2001</a></li>
  4. <li><a href="#2002">2002</a></li>
  5. <li><a href="#2003">2003</a></li>
  6. <li><a href="#2004">2004</a></li>
  7. <li><a href="#2005">2005</a></li>
  8. <li><a href="#2006">2006</a></li>
  9. <li><a href="#2007">2007</a></li>
  10. </ul>
  11. <ul >
  12. <li >
  13. <img class="fixie" width="256" height="256" />
  14. <h1>2001</h1>
  15. <p class="fixie"></p>
  16. </li>
  17. <li >
  18. <img class="fixie" width="256" height="256" />
  19. <h1>2002</h1>
  20. <p class="fixie"></p>
  21. </li>
  22. <li >
  23. <img class="fixie" width="256" height="256" />
  24. <h1>2003</h1>
  25. <p class="fixie"></p>
  26. </li>
  27. <li >
  28. <img class="fixie" width="256" height="256" />
  29. <h1>2004</h1>
  30. <p class="fixie"></p>
  31. </li>
  32. <li >
  33. <img class="fixie" width="256" height="256" />
  34. <h1>2005</h1>
  35. <p class="fixie"></p>
  36. </li>
  37. <li >
  38. <img class="fixie" width="256" height="256" />
  39. <h1>2006</h1>
  40. <p class="fixie"></p>
  41. </li>
  42. <li >
  43. <img class="fixie" width="256" height="256" />
  44. <h1>2007</h1>
  45. <p class="fixie"></p>
  46. </li>
  47. </ul>
  48. <div ></div>
  49. <div ></div>
  50. <a href="#" >+</a>
  51. <a href="#" >-</a>
  52. </div>

CSS代码

不同方向展示的时间轴,使用不同的样式文件,这里我们只列出水平时间轴样式:

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. background: #222;
  7. font-family: Georgia, serif;
  8. color: #707070;
  9. font-size: 14px;
  10. }
  11. a {
  12. color: #404040;
  13. text-decoration: none;
  14. -webkit-transition: 0.5s;
  15. -moz-transition: 0.5s;
  16. -o-transition: 0.5s;
  17. -ms-transition: 0.5s;
  18. transition: 0.5s;
  19. }
  20. a:hover,
  21. a.selected {
  22. color: #808080;
  23. }
  24. h1,h2,h4,h5,h6 {
  25. text-align: center;
  26. color: #ccc;
  27. text-shadow: #000 1px 1px 2px;
  28. margin-bottom: 5px;
  29. }
  30. h1 {
  31. font-size: 18px;
  32. }
  33. h2 {
  34. font-size: 14px;
  35. }
  36. .sociales {
  37. text-align: center;
  38. margin-bottom: 20px;
  39. }
  40. #timeline {
  41. width: 788px;
  42. height: 350px;
  43. overflow: hidden;
  44. margin: 100px auto;
  45. position: relative;
  46. background: url('../images/dot.png') left 45px repeat-x;
  47. }
  48. #dates {
  49. width: 120px;
  50. height: 60px;
  51. overflow: hidden;
  52. }
  53. #dates li {
  54. list-style: none;
  55. float: left;
  56. width: 150px;
  57. height: 50px;
  58. font-size: 24px;
  59. text-align: center;
  60. background: url('../images/bdot.png') center bottom no-repeat;
  61. }
  62. #dates a {
  63. line-height: 38px;
  64. padding-bottom: 10px;
  65. color: #CCC;
  66. }
  67. #dates .selected {
  68. font-size: 30px;
  69. color: #5DB0E6;
  70. padding-bottom: 12px;
  71. background: url('../images/bdot1.png') center bottom no-repeat;
  72. }
  73. #issues {
  74. width: 788px;
  75. height: 350px;
  76. overflow: hidden;
  77. }
  78. #issues li {
  79. width: 788px;
  80. height: 350px;
  81. list-style: none;
  82. float: left;
  83. }
  84. #issues li.selected img {
  85. -webkit-transform: scale(1.1,1.1);
  86. -moz-transform: scale(1.1,1.1);
  87. -o-transform: scale(1.1,1.1);
  88. -ms-transform: scale(1.1,1.1);
  89. transform: scale(1.1,1.1);
  90. }
  91. #issues li img {
  92. float: left;
  93. margin: 10px 30px 10px 50px;
  94. background: transparent;
  95. -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#00FFFFFF,endColorstr=#00FFFFFF)"; /* IE 8 */
  96. filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#00FFFFFF,endColorstr=#00FFFFFF);/* IE 6 & 7 */
  97. zoom: 1;
  98. -webkit-transition: all 2s ease-in-out;
  99. -moz-transition: all 2s ease-in-out;
  100. -o-transition: all 2s ease-in-out;
  101. -ms-transition: all 2s ease-in-out;
  102. transition: all 2s ease-in-out;
  103. -webkit-transform: scale(0.7,0.7);
  104. -moz-transform: scale(0.7,0.7);
  105. -o-transform: scale(0.7,0.7);
  106. -ms-transform: scale(0.7,0.7);
  107. transform: scale(0.7,0.7);
  108. }
  109. #issues li h1 {
  110. color: #5DB0E6;
  111. font-size: 48px;
  112. margin: 20px 0;
  113. text-shadow: #000 1px 1px 2px;
  114. }
  115. #issues li p {
  116. font-size: 14px;
  117. margin-right: 70px;
  118. font-weight: normal;
  119. line-height: 22px;
  120. text-shadow: #000 1px 1px 2px;
  121. }
  122. #grad_left,
  123. #grad_right {
  124. width: 100px;
  125. height: 350px;
  126. position: absolute;
  127. top: 0;
  128. }
  129. #grad_left {
  130. left: 0;
  131. background: url('../images/grad_left.png') repeat-y;
  132. }
  133. #grad_right {
  134. right: 0;
  135. background: url('../images/grad_right.png') repeat-y;
  136. }
  137. #next,
  138. #prev {
  139. position: absolute;
  140. top: 0;
  141. font-size: 70px;
  142. top: 170px;
  143. width: 22px;
  144. height: 38px;
  145. background-position: 0 0;
  146. background-repeat: no-repeat;
  147. text-indent: -9999px;
  148. overflow: hidden;
  149. }
  150. #next:hover,
  151. #prev:hover {
  152. background-position: 0 -76px;
  153. }
  154. #next {
  155. right: 0;
  156. background-image: url('../images/next.png');
  157. }
  158. #prev {
  159. left: 0;
  160. background-image: url('../images/prev.png');
  161. }
  162. #next.disabled,
  163. #prev.disabled {
  164. opacity: 0.2;
  165. }

全部代码书写完毕, 大家可以查看在线演示来浏览效果,如果你有更好的建议,请给我们留言,谢谢!

原文链接:http://www.gbtags.com/gb/share/5862.htm