Skip to content

Instantly share code, notes, and snippets.

@jakemmarsh
Created February 12, 2014 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakemmarsh/8965663 to your computer and use it in GitHub Desktop.
Save jakemmarsh/8965663 to your computer and use it in GitHub Desktop.
AngularJS Directive for looping through and displaying multiple headers
directives.directive('headerSwitcher', function() {
return {
restrict: 'A',
scope: {
elementType: '='
},
link: function(scope, element, attrs) {
var headers = $(element).children(scope.elementType),
headerIndex = -1;
function showNextHeader() {
++headerIndex;
// show first header immediately
if(headerIndex === 0) {
headers.eq(headerIndex % headers.length)
.show()
.delay(2500)
.fadeOut(1000, showNextHeader);
}
// then fade them in
else {
headers.eq(headerIndex % headers.length)
.fadeIn(1000)
.delay(2500)
.fadeOut(1000, showNextHeader);
}
}
showNextHeader();
}
};
});
<div header-switcher="h1">
<h1 style="display:none">First Header</h1>
<h1 style="display:none">Second Header</h1>
<h1 style="display:none">Third Header</h1>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment