使用jQuery timelinr和animate.css建立超酷的CSS動畫時間軸特效

在過去咱們的文章中,咱們介紹過不少不錯的時間軸插件,使用這些超棒的插件能夠幫助你建立動感漂亮的時間軸展現,其中比較不錯的插件以下:javascript

使用以上jQuery插件或者類庫均可以建立漂亮的時間軸timline特效。css

因爲在咱們的gbtags.com社區開發中,咱們須要建立一個簡單實用的用戶時間軸應用,這裏咱們選擇使用Timelinr來開發,而且爲了使得動畫過程更加豐富和有趣咱們同時使用Animate.css來建立各類不一樣的CSS動畫特效。html

須要使用到的第三方插件和CSS類庫以下:java

Timelinr是一個時間軸的jQuery插件實現,你能夠方便的使用它來生成一個動態的時間軸特效,提供了垂直和水平顯示方式,而且支持自動播放。jquery

Animate.css是由Dan Eden開發的一套超棒的CSS動畫類庫,幫助你使用純CSS來實現各類不一樣的動畫特效。前面咱們曾經專題介紹過Animate.css,若是你不瞭解,請閱讀這篇文章:超棒的跨瀏覽器純CSS動畫實現 - Animate.csscss3

fixie.js在咱們之前的文章中介紹過,是一個自動幫助你填充內容的迷你類庫,若是你厭倦了拷貝一堆內容的話,能夠使用它來自動生成內容,我的很是喜歡,這裏我將使用它來生成圖片和文字內容。web

這裏咱們使用cufon在生成更加個性化的年份,cufon.js會將制定的文字轉化爲畫布圖片。瀏覽器

Javascript代碼

由於jQuery timelinr是一個插件,爲了更好的封裝它,咱們這裏直接修改插件內容,缺省的動畫效果比較簡單,只是在每一個頁面以放大的方式來展現圖片,咱們但願可以添加更多特效,這裏經過添加animate.css中定義的動畫class來實現。框架

使用animate很是簡單,你只須要使用jQuery的addClass方法調用對應的類,便可實現CSS動畫效果,以下:jsp

  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 id="timeline">
  2. <ul id="dates">
  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 id="issues">
  12. <li id="2001">
  13. <img class="fixie" width="256" height="256" />
  14. <h1>2001</h1>
  15. <p class="fixie"></p>
  16. </li>
  17. <li id="2002">
  18. <img class="fixie" width="256" height="256" />
  19. <h1>2002</h1>
  20. <p class="fixie"></p>
  21. </li>
  22. <li id="2003">
  23. <img class="fixie" width="256" height="256" />
  24. <h1>2003</h1>
  25. <p class="fixie"></p>
  26. </li>
  27. <li id="2004">
  28. <img class="fixie" width="256" height="256" />
  29. <h1>2004</h1>
  30. <p class="fixie"></p>
  31. </li>
  32. <li id="2005">
  33. <img class="fixie" width="256" height="256" />
  34. <h1>2005</h1>
  35. <p class="fixie"></p>
  36. </li>
  37. <li id="2006">
  38. <img class="fixie" width="256" height="256" />
  39. <h1>2006</h1>
  40. <p class="fixie"></p>
  41. </li>
  42. <li id="2007">
  43. <img class="fixie" width="256" height="256" />
  44. <h1>2007</h1>
  45. <p class="fixie"></p>
  46. </li>
  47. </ul>
  48. <div id="grad_left"></div>
  49. <div id="grad_right"></div>
  50. <a href="#" id="next">+</a>
  51. <a href="#" id="prev">-</a>
  52. </div>
 

 

CSS代碼

不一樣方向展現的時間軸,使用不一樣的樣式文件,這裏咱們只列出水平時間軸樣式:

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

 

所有代碼書寫完畢, 你們能夠查看在線演示來瀏覽效果,若是你有更好的建議,請給咱們留言,謝謝!

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

相關文章
相關標籤/搜索