jquery(document).ready(function($){ //set animation timing var animationdelay = 2500, //loading bar effect baranimationdelay = 3800, barwaiting = baranimationdelay - 3000, //3000 is the duration of the transition on the loading bar - set in the scss/css file //letters effect lettersdelay = 50, //type effect typelettersdelay = 150, selectionduration = 500, typeanimationdelay = selectionduration + 800, //clip effect revealduration = 600, revealanimationdelay = 1500; initheadline(); function initheadline() { //insert element for each letter of a changing word singleletters($('.cd-headline.letters').find('b')); //initialise headline animation animateheadline($('.cd-headline')); } function singleletters($words) { $words.each(function(){ var word = $(this), letters = word.text().split(''), selected = word.hasclass('is-visible'); for (i in letters) { if(word.parents('.rotate-2').length > 0) letters[i] = '' + letters[i] + ''; letters[i] = (selected) ? '' + letters[i] + '': '' + letters[i] + ''; } var newletters = letters.join(''); word.html(newletters).css('opacity', 1); }); } function animateheadline($headlines) { var duration = animationdelay; $headlines.each(function(){ var headline = $(this); if(headline.hasclass('loading-bar')) { duration = baranimationdelay; settimeout(function(){ headline.find('.cd-words-wrapper').addclass('is-loading') }, barwaiting); } else if (headline.hasclass('clip')){ var spanwrapper = headline.find('.cd-words-wrapper'), newwidth = spanwrapper.width() + 10 spanwrapper.css('width', newwidth); } else if (!headline.hasclass('type') ) { //assign to .cd-words-wrapper the width of its longest word var words = headline.find('.cd-words-wrapper b'), width = 0; words.each(function(){ var wordwidth = $(this).width(); if (wordwidth > width) width = wordwidth; }); headline.find('.cd-words-wrapper').css('width', width); }; //trigger animation settimeout(function(){ hideword( headline.find('.is-visible').eq(0) ) }, duration); }); } function hideword($word) { var nextword = takenext($word); if($word.parents('.cd-headline').hasclass('type')) { var parentspan = $word.parent('.cd-words-wrapper'); parentspan.addclass('selected').removeclass('waiting'); settimeout(function(){ parentspan.removeclass('selected'); $word.removeclass('is-visible').addclass('is-hidden').children('i').removeclass('in').addclass('out'); }, selectionduration); settimeout(function(){ showword(nextword, typelettersdelay) }, typeanimationdelay); } else if($word.parents('.cd-headline').hasclass('letters')) { var bool = ($word.children('i').length >= nextword.children('i').length) ? true : false; hideletter($word.find('i').eq(0), $word, bool, lettersdelay); showletter(nextword.find('i').eq(0), nextword, bool, lettersdelay); } else if($word.parents('.cd-headline').hasclass('clip')) { $word.parents('.cd-words-wrapper').animate({ width : '2px' }, revealduration, function(){ switchword($word, nextword); showword(nextword); }); } else if ($word.parents('.cd-headline').hasclass('loading-bar')){ $word.parents('.cd-words-wrapper').removeclass('is-loading'); switchword($word, nextword); settimeout(function(){ hideword(nextword) }, baranimationdelay); settimeout(function(){ $word.parents('.cd-words-wrapper').addclass('is-loading') }, barwaiting); } else { switchword($word, nextword); settimeout(function(){ hideword(nextword) }, animationdelay); } } function showword($word, $duration) { if($word.parents('.cd-headline').hasclass('type')) { showletter($word.find('i').eq(0), $word, false, $duration); $word.addclass('is-visible').removeclass('is-hidden'); } else if($word.parents('.cd-headline').hasclass('clip')) { $word.parents('.cd-words-wrapper').animate({ 'width' : $word.width() + 10 }, revealduration, function(){ settimeout(function(){ hideword($word) }, revealanimationdelay); }); } } function hideletter($letter, $word, $bool, $duration) { $letter.removeclass('in').addclass('out'); if(!$letter.is(':last-child')) { settimeout(function(){ hideletter($letter.next(), $word, $bool, $duration); }, $duration); } else if($bool) { settimeout(function(){ hideword(takenext($word)) }, animationdelay); } if($letter.is(':last-child') && $('html').hasclass('no-csstransitions')) { var nextword = takenext($word); switchword($word, nextword); } } function showletter($letter, $word, $bool, $duration) { $letter.addclass('in').removeclass('out'); if(!$letter.is(':last-child')) { settimeout(function(){ showletter($letter.next(), $word, $bool, $duration); }, $duration); } else { if($word.parents('.cd-headline').hasclass('type')) { settimeout(function(){ $word.parents('.cd-words-wrapper').addclass('waiting'); }, 200);} if(!$bool) { settimeout(function(){ hideword($word) }, animationdelay) } } } function takenext($word) { return (!$word.is(':last-child')) ? $word.next() : $word.parent().children().eq(0); } function takeprev($word) { return (!$word.is(':first-child')) ? $word.prev() : $word.parent().children().last(); } function switchword($oldword, $newword) { $oldword.removeclass('is-visible').addclass('is-hidden'); $newword.removeclass('is-hidden').addclass('is-visible'); } });