No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

animated-headline.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. jQuery(document).ready(function($){
  2. //set animation timing
  3. var animationDelay = 2500,
  4. //loading bar effect
  5. barAnimationDelay = 3800,
  6. barWaiting = barAnimationDelay - 3000, //3000 is the duration of the transition on the loading bar - set in the scss/css file
  7. //letters effect
  8. lettersDelay = 50,
  9. //type effect
  10. typeLettersDelay = 150,
  11. selectionDuration = 500,
  12. typeAnimationDelay = selectionDuration + 800,
  13. //clip effect
  14. revealDuration = 600,
  15. revealAnimationDelay = 1500;
  16. initHeadline();
  17. function initHeadline() {
  18. //insert <i> element for each letter of a changing word
  19. singleLetters($('.cd-headline.letters').find('b'));
  20. //initialise headline animation
  21. animateHeadline($('.cd-headline'));
  22. }
  23. function singleLetters($words) {
  24. $words.each(function(){
  25. var word = $(this),
  26. letters = word.text().split(''),
  27. selected = word.hasClass('is-visible');
  28. for (i in letters) {
  29. if(word.parents('.rotate-2').length > 0) letters[i] = '<em>' + letters[i] + '</em>';
  30. letters[i] = (selected) ? '<i class="in">' + letters[i] + '</i>': '<i>' + letters[i] + '</i>';
  31. }
  32. var newLetters = letters.join('');
  33. word.html(newLetters).css('opacity', 1);
  34. });
  35. }
  36. function animateHeadline($headlines) {
  37. var duration = animationDelay;
  38. $headlines.each(function(){
  39. var headline = $(this);
  40. if(headline.hasClass('loading-bar')) {
  41. duration = barAnimationDelay;
  42. setTimeout(function(){ headline.find('.cd-words-wrapper').addClass('is-loading') }, barWaiting);
  43. } else if (headline.hasClass('clip')){
  44. var spanWrapper = headline.find('.cd-words-wrapper'),
  45. newWidth = spanWrapper.width() + 10
  46. spanWrapper.css('width', newWidth);
  47. } else if (!headline.hasClass('type') ) {
  48. //assign to .cd-words-wrapper the width of its longest word
  49. var words = headline.find('.cd-words-wrapper b'),
  50. width = 0;
  51. words.each(function(){
  52. var wordWidth = $(this).width();
  53. if (wordWidth > width) width = wordWidth;
  54. });
  55. headline.find('.cd-words-wrapper').css('width', width);
  56. };
  57. //trigger animation
  58. setTimeout(function(){ hideWord( headline.find('.is-visible').eq(0) ) }, duration);
  59. });
  60. }
  61. function hideWord($word) {
  62. var nextWord = takeNext($word);
  63. if($word.parents('.cd-headline').hasClass('type')) {
  64. var parentSpan = $word.parent('.cd-words-wrapper');
  65. parentSpan.addClass('selected').removeClass('waiting');
  66. setTimeout(function(){
  67. parentSpan.removeClass('selected');
  68. $word.removeClass('is-visible').addClass('is-hidden').children('i').removeClass('in').addClass('out');
  69. }, selectionDuration);
  70. setTimeout(function(){ showWord(nextWord, typeLettersDelay) }, typeAnimationDelay);
  71. } else if($word.parents('.cd-headline').hasClass('letters')) {
  72. var bool = ($word.children('i').length >= nextWord.children('i').length) ? true : false;
  73. hideLetter($word.find('i').eq(0), $word, bool, lettersDelay);
  74. showLetter(nextWord.find('i').eq(0), nextWord, bool, lettersDelay);
  75. } else if($word.parents('.cd-headline').hasClass('clip')) {
  76. $word.parents('.cd-words-wrapper').animate({ width : '2px' }, revealDuration, function(){
  77. switchWord($word, nextWord);
  78. showWord(nextWord);
  79. });
  80. } else if ($word.parents('.cd-headline').hasClass('loading-bar')){
  81. $word.parents('.cd-words-wrapper').removeClass('is-loading');
  82. switchWord($word, nextWord);
  83. setTimeout(function(){ hideWord(nextWord) }, barAnimationDelay);
  84. setTimeout(function(){ $word.parents('.cd-words-wrapper').addClass('is-loading') }, barWaiting);
  85. } else {
  86. switchWord($word, nextWord);
  87. setTimeout(function(){ hideWord(nextWord) }, animationDelay);
  88. }
  89. }
  90. function showWord($word, $duration) {
  91. if($word.parents('.cd-headline').hasClass('type')) {
  92. showLetter($word.find('i').eq(0), $word, false, $duration);
  93. $word.addClass('is-visible').removeClass('is-hidden');
  94. } else if($word.parents('.cd-headline').hasClass('clip')) {
  95. $word.parents('.cd-words-wrapper').animate({ 'width' : $word.width() + 10 }, revealDuration, function(){
  96. setTimeout(function(){ hideWord($word) }, revealAnimationDelay);
  97. });
  98. }
  99. }
  100. function hideLetter($letter, $word, $bool, $duration) {
  101. $letter.removeClass('in').addClass('out');
  102. if(!$letter.is(':last-child')) {
  103. setTimeout(function(){ hideLetter($letter.next(), $word, $bool, $duration); }, $duration);
  104. } else if($bool) {
  105. setTimeout(function(){ hideWord(takeNext($word)) }, animationDelay);
  106. }
  107. if($letter.is(':last-child') && $('html').hasClass('no-csstransitions')) {
  108. var nextWord = takeNext($word);
  109. switchWord($word, nextWord);
  110. }
  111. }
  112. function showLetter($letter, $word, $bool, $duration) {
  113. $letter.addClass('in').removeClass('out');
  114. if(!$letter.is(':last-child')) {
  115. setTimeout(function(){ showLetter($letter.next(), $word, $bool, $duration); }, $duration);
  116. } else {
  117. if($word.parents('.cd-headline').hasClass('type')) { setTimeout(function(){ $word.parents('.cd-words-wrapper').addClass('waiting'); }, 200);}
  118. if(!$bool) { setTimeout(function(){ hideWord($word) }, animationDelay) }
  119. }
  120. }
  121. function takeNext($word) {
  122. return (!$word.is(':last-child')) ? $word.next() : $word.parent().children().eq(0);
  123. }
  124. function takePrev($word) {
  125. return (!$word.is(':first-child')) ? $word.prev() : $word.parent().children().last();
  126. }
  127. function switchWord($oldWord, $newWord) {
  128. $oldWord.removeClass('is-visible').addClass('is-hidden');
  129. $newWord.removeClass('is-hidden').addClass('is-visible');
  130. }
  131. });