jquery.timelinr-0.9.5.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* ----------------------------------
  2. jQuery Timelinr 0.9.5
  3. tested with jQuery v1.6+
  4. Copyright 2011, CSSLab.cl
  5. Free under the MIT license.
  6. http://www.opensource.org/licenses/mit-license.php
  7. instructions: http://www.csslab.cl/2011/08/18/jquery-timelinr/
  8. ---------------------------------- */
  9. jQuery.fn.timelinr = function(options){
  10. // default plugin settings
  11. settings = jQuery.extend({
  12. orientation: 'horizontal', // value: horizontal | vertical, default to horizontal
  13. containerDiv: '#timeline', // value: any HTML tag or #id, default to #timeline
  14. datesDiv: '#dates', // value: any HTML tag or #id, default to #dates
  15. datesSelectedClass: 'selected', // value: any class, default to selected
  16. datesSpeed: 'normal', // value: integer between 100 and 1000 (recommended) or 'slow', 'normal' or 'fast'; default to normal
  17. issuesDiv: '#issues', // value: any HTML tag or #id, default to #issues
  18. issuesSelectedClass: 'selected', // value: any class, default to selected
  19. issuesSpeed: 'fast', // value: integer between 100 and 1000 (recommended) or 'slow', 'normal' or 'fast'; default to fast
  20. issuesTransparency: 0.2, // value: integer between 0 and 1 (recommended), default to 0.2
  21. issuesTransparencySpeed: 500, // value: integer between 100 and 1000 (recommended), default to 500 (normal)
  22. prevButton: '#prev', // value: any HTML tag or #id, default to #prev
  23. nextButton: '#next', // value: any HTML tag or #id, default to #next
  24. arrowKeys: 'false', // value: true | false, default to false
  25. startAt: 1, // value: integer, default to 1 (first)
  26. autoPlay: 'false', // value: true | false, default to false
  27. autoPlayDirection: 'forward', // value: forward | backward, default to forward
  28. autoPlayPause: 2000 // value: integer (1000 = 1 seg), default to 2000 (2segs)
  29. }, options);
  30. $(function(){
  31. // setting variables... many of them
  32. var howManyDates = $(settings.datesDiv+' li').length;
  33. var howManyIssues = $(settings.issuesDiv+' li').length;
  34. var currentDate = $(settings.datesDiv).find('a.'+settings.datesSelectedClass);
  35. var currentIssue = $(settings.issuesDiv).find('li.'+settings.issuesSelectedClass);
  36. var widthContainer = $(settings.containerDiv).width();
  37. var heightContainer = $(settings.containerDiv).height();
  38. var widthIssues = $(settings.issuesDiv).width();
  39. var heightIssues = $(settings.issuesDiv).height();
  40. var widthIssue = $(settings.issuesDiv+' li').width();
  41. var heightIssue = $(settings.issuesDiv+' li').height();
  42. var widthDates = $(settings.datesDiv).width();
  43. var heightDates = $(settings.datesDiv).height();
  44. var widthDate = $(settings.datesDiv+' li').width();
  45. var heightDate = $(settings.datesDiv+' li').height();
  46. // set positions!
  47. if(settings.orientation == 'horizontal') {
  48. $(settings.issuesDiv).width(widthIssue*howManyIssues);
  49. $(settings.datesDiv).width(widthDate*howManyDates).css('marginLeft',widthContainer/2-widthDate/2);
  50. var defaultPositionDates = parseInt($(settings.datesDiv).css('marginLeft').substring(0,$(settings.datesDiv).css('marginLeft').indexOf('px')));
  51. } else if(settings.orientation == 'vertical') {
  52. $(settings.issuesDiv).height(heightIssue*howManyIssues);
  53. $(settings.datesDiv).height(heightDate*howManyDates).css('marginTop',heightContainer/2-heightDate/2);
  54. var defaultPositionDates = parseInt($(settings.datesDiv).css('marginTop').substring(0,$(settings.datesDiv).css('marginTop').indexOf('px')));
  55. }
  56. $(settings.datesDiv+' a').click(function(event){
  57. event.preventDefault();
  58. // first vars
  59. var whichIssue = $(this).text();
  60. var currentIndex = $(this).parent().prevAll().length;
  61. // moving the elements
  62. if(settings.orientation == 'horizontal') {
  63. $(settings.issuesDiv).animate({'marginLeft':-widthIssue*currentIndex},{queue:false, duration:settings.issuesSpeed});
  64. } else if(settings.orientation == 'vertical') {
  65. $(settings.issuesDiv).animate({'marginTop':-heightIssue*currentIndex},{queue:false, duration:settings.issuesSpeed});
  66. }
  67. $(settings.issuesDiv+' li').animate({'opacity':settings.issuesTransparency},{queue:false, duration:settings.issuesSpeed}).removeClass(settings.issuesSelectedClass).eq(currentIndex).addClass(settings.issuesSelectedClass).fadeTo(settings.issuesTransparencySpeed,1);
  68. // now moving the dates
  69. $(settings.datesDiv+' a').removeClass(settings.datesSelectedClass);
  70. $(this).addClass(settings.datesSelectedClass);
  71. if(settings.orientation == 'horizontal') {
  72. $(settings.datesDiv).animate({'marginLeft':defaultPositionDates-(widthDate*currentIndex)},{queue:false, duration:'settings.datesSpeed'});
  73. } else if(settings.orientation == 'vertical') {
  74. $(settings.datesDiv).animate({'marginTop':defaultPositionDates-(heightDate*currentIndex)},{queue:false, duration:'settings.datesSpeed'});
  75. }
  76. });
  77. $(settings.nextButton).bind('click', function(event){
  78. event.preventDefault();
  79. if(settings.orientation == 'horizontal') {
  80. var currentPositionIssues = parseInt($(settings.issuesDiv).css('marginLeft').substring(0,$(settings.issuesDiv).css('marginLeft').indexOf('px')));
  81. var currentIssueIndex = currentPositionIssues/widthIssue;
  82. var currentPositionDates = parseInt($(settings.datesDiv).css('marginLeft').substring(0,$(settings.datesDiv).css('marginLeft').indexOf('px')));
  83. var currentIssueDate = currentPositionDates-widthDate;
  84. if(currentPositionIssues <= -(widthIssue*howManyIssues-(widthIssue))) {
  85. $(settings.issuesDiv).stop();
  86. $(settings.datesDiv+' li:last-child a').click();
  87. } else {
  88. if (!$(settings.issuesDiv).is(':animated')) {
  89. $(settings.issuesDiv).animate({'marginLeft':currentPositionIssues-widthIssue},{queue:false, duration:settings.issuesSpeed});
  90. $(settings.issuesDiv+' li').animate({'opacity':settings.issuesTransparency},{queue:false, duration:settings.issuesSpeed});
  91. $(settings.issuesDiv+' li.'+settings.issuesSelectedClass).removeClass(settings.issuesSelectedClass).next().fadeTo(settings.issuesTransparencySpeed, 1).addClass(settings.issuesSelectedClass);
  92. $(settings.datesDiv).animate({'marginLeft':currentIssueDate},{queue:false, duration:'settings.datesSpeed'});
  93. $(settings.datesDiv+' a.'+settings.datesSelectedClass).removeClass(settings.datesSelectedClass).parent().next().children().addClass(settings.datesSelectedClass);
  94. }
  95. }
  96. } else if(settings.orientation == 'vertical') {
  97. var currentPositionIssues = parseInt($(settings.issuesDiv).css('marginTop').substring(0,$(settings.issuesDiv).css('marginTop').indexOf('px')));
  98. var currentIssueIndex = currentPositionIssues/heightIssue;
  99. var currentPositionDates = parseInt($(settings.datesDiv).css('marginTop').substring(0,$(settings.datesDiv).css('marginTop').indexOf('px')));
  100. var currentIssueDate = currentPositionDates-heightDate;
  101. if(currentPositionIssues <= -(heightIssue*howManyIssues-(heightIssue))) {
  102. $(settings.issuesDiv).stop();
  103. $(settings.datesDiv+' li:last-child a').click();
  104. } else {
  105. if (!$(settings.issuesDiv).is(':animated')) {
  106. $(settings.issuesDiv).animate({'marginTop':currentPositionIssues-heightIssue},{queue:false, duration:settings.issuesSpeed});
  107. $(settings.issuesDiv+' li').animate({'opacity':settings.issuesTransparency},{queue:false, duration:settings.issuesSpeed});
  108. $(settings.issuesDiv+' li.'+settings.issuesSelectedClass).removeClass(settings.issuesSelectedClass).next().fadeTo(settings.issuesTransparencySpeed, 1).addClass(settings.issuesSelectedClass);
  109. $(settings.datesDiv).animate({'marginTop':currentIssueDate},{queue:false, duration:'settings.datesSpeed'});
  110. $(settings.datesDiv+' a.'+settings.datesSelectedClass).removeClass(settings.datesSelectedClass).parent().next().children().addClass(settings.datesSelectedClass);
  111. }
  112. }
  113. }
  114. });
  115. $(settings.prevButton).click(function(event){
  116. event.preventDefault();
  117. if(settings.orientation == 'horizontal') {
  118. var currentPositionIssues = parseInt($(settings.issuesDiv).css('marginLeft').substring(0,$(settings.issuesDiv).css('marginLeft').indexOf('px')));
  119. var currentIssueIndex = currentPositionIssues/widthIssue;
  120. var currentPositionDates = parseInt($(settings.datesDiv).css('marginLeft').substring(0,$(settings.datesDiv).css('marginLeft').indexOf('px')));
  121. var currentIssueDate = currentPositionDates+widthDate;
  122. if(currentPositionIssues >= 0) {
  123. $(settings.issuesDiv).stop();
  124. $(settings.datesDiv+' li:first-child a').click();
  125. } else {
  126. if (!$(settings.issuesDiv).is(':animated')) {
  127. $(settings.issuesDiv).animate({'marginLeft':currentPositionIssues+widthIssue},{queue:false, duration:settings.issuesSpeed});
  128. $(settings.issuesDiv+' li').animate({'opacity':settings.issuesTransparency},{queue:false, duration:settings.issuesSpeed});
  129. $(settings.issuesDiv+' li.'+settings.issuesSelectedClass).removeClass(settings.issuesSelectedClass).prev().fadeTo(settings.issuesTransparencySpeed, 1).addClass(settings.issuesSelectedClass);
  130. $(settings.datesDiv).animate({'marginLeft':currentIssueDate},{queue:false, duration:'settings.datesSpeed'});
  131. $(settings.datesDiv+' a.'+settings.datesSelectedClass).removeClass(settings.datesSelectedClass).parent().prev().children().addClass(settings.datesSelectedClass);
  132. }
  133. }
  134. } else if(settings.orientation == 'vertical') {
  135. var currentPositionIssues = parseInt($(settings.issuesDiv).css('marginTop').substring(0,$(settings.issuesDiv).css('marginTop').indexOf('px')));
  136. var currentIssueIndex = currentPositionIssues/heightIssue;
  137. var currentPositionDates = parseInt($(settings.datesDiv).css('marginTop').substring(0,$(settings.datesDiv).css('marginTop').indexOf('px')));
  138. var currentIssueDate = currentPositionDates+heightDate;
  139. if(currentPositionIssues >= 0) {
  140. $(settings.issuesDiv).stop();
  141. $(settings.datesDiv+' li:first-child a').click();
  142. } else {
  143. if (!$(settings.issuesDiv).is(':animated')) {
  144. $(settings.issuesDiv).animate({'marginTop':currentPositionIssues+heightIssue},{queue:false, duration:settings.issuesSpeed});
  145. $(settings.issuesDiv+' li').animate({'opacity':settings.issuesTransparency},{queue:false, duration:settings.issuesSpeed});
  146. $(settings.issuesDiv+' li.'+settings.issuesSelectedClass).removeClass(settings.issuesSelectedClass).prev().fadeTo(settings.issuesTransparencySpeed, 1).addClass(settings.issuesSelectedClass);
  147. $(settings.datesDiv).animate({'marginTop':currentIssueDate},{queue:false, duration:'settings.datesSpeed'},{queue:false, duration:settings.issuesSpeed});
  148. $(settings.datesDiv+' a.'+settings.datesSelectedClass).removeClass(settings.datesSelectedClass).parent().prev().children().addClass(settings.datesSelectedClass);
  149. }
  150. }
  151. }
  152. });
  153. // keyboard navigation, added since 0.9.1
  154. if(settings.arrowKeys=='true') {
  155. if(settings.orientation=='horizontal') {
  156. $(document).keydown(function(event){
  157. if (event.keyCode == 39) {
  158. $(settings.nextButton).click();
  159. }
  160. if (event.keyCode == 37) {
  161. $(settings.prevButton).click();
  162. }
  163. });
  164. } else if(settings.orientation=='vertical') {
  165. $(document).keydown(function(event){
  166. if (event.keyCode == 40) {
  167. $(settings.nextButton).click();
  168. }
  169. if (event.keyCode == 38) {
  170. $(settings.prevButton).click();
  171. }
  172. });
  173. }
  174. }
  175. // default position startAt, added since 0.9.3
  176. $(settings.datesDiv+' li').eq(settings.startAt-1).find('a').trigger('click');
  177. // autoPlay, added since 0.9.4
  178. if(settings.autoPlay == 'true') {
  179. setInterval("autoPlay()", settings.autoPlayPause);
  180. }
  181. });
  182. };
  183. // autoPlay, added since 0.9.4
  184. function autoPlay(){
  185. var currentDate = $(settings.datesDiv).find('a.'+settings.datesSelectedClass);
  186. if(settings.autoPlayDirection == 'forward') {
  187. if(currentDate.parent().is('li:last-child')) {
  188. $(settings.datesDiv+' li:first-child').find('a').trigger('click');
  189. } else {
  190. currentDate.parent().next().find('a').trigger('click');
  191. }
  192. } else if(settings.autoPlayDirection == 'backward') {
  193. if(currentDate.parent().is('li:first-child')) {
  194. $(settings.datesDiv+' li:last-child').find('a').trigger('click');
  195. } else {
  196. currentDate.parent().prev().find('a').trigger('click');
  197. }
  198. }
  199. }