/* * Animate-plus.js * Version 0.1 * A jQuery plugin that extends animate.css (http://daneden.github.io/animate.css/) functionality. * * Author: Telmo Marques * Email: me@telmo.pt * Twitter: @_TelmoMarques */ (function($) { /** * Force an element to redraw * Author: Matt Tortolani * Source: http://forrst.com/posts/jQuery_redraw-BGv */ jQuery.fn.redraw = function() { return this.hide(0, function(){$(this).show();}); }; /** * Restart a group's animation */ jQuery.fn.restartAnimation = function() { var groupName = $(this).attr("data-animation-group"); var group = AnimatePlus.getInstance().getMap()[groupName]; group.restart(); }; /** * Defines an HTML element that will be animated * @param htmlElement Reference to an html element */ function Element(htmlElement) { /* * Constructor */ //Original html element this.htmlElement = htmlElement; //List of animations to apply to this element this.animations = $(this.htmlElement).attr("data-animations").split(","); //Keeps animation index (in practice this.animations[this.animationIndex]); this.animationIndex = 0; //Keeps last performed animation (animate.css class name) this.lastAnimation = ""; //Check if this element defines animation durations if($(this.htmlElement).attr("data-animation-duration") !== undefined) { //Get the values this.animationDuration = $(this.htmlElement).attr("data-animation-duration").split(","); } //Check if this element defines animation delays if($(this.htmlElement).attr("data-animation-delay") !== undefined) { //Get the values this.animationDelay = $(this.htmlElement).attr("data-animation-delay").split(","); } }; /* * Performs the next animation on the element * @param {function} callback Function to be called once the animation is complete */ Element.prototype.doNextAnimation = function(callback) { //Check if this is the last animation if(this.animationIndex >= this.animations.length) { //All done callback(); return; } //Duration of animation var duration; //Delay of animation var delay; //Remove last animation from the html element (defined by an animate.css class) $(this.htmlElement).removeClass(this.lastAnimation); //Get the next animation css class name this.lastAnimation = this.animations[this.animationIndex]; //Clear duration $(this.htmlElement).css("animation-duration", "").css("-webkit-animation-duration", "").css("-moz-animation-duration", "").css("-ms-animation-duration", "").css("-o-animation-duration", ""); //If we defined a duration for this animation... if(this.animationDuration !== undefined && this.animationDuration[this.animationIndex] !== undefined) { //... set it duration = this.animationDuration[this.animationIndex]; $(this.htmlElement).css("animation-duration", duration).css("-webkit-animation-duration", duration).css("-moz-animation-duration", duration).css("-ms-animation-duration", duration).css("-o-animation-duration", duration); } //Clear delay $(this.htmlElement).css("animation-delay", "").css("-webkit-animation-delay", "").css("-moz-animation-delay", "").css("-ms-animation-delay", "").css("-o-animation-delay", ""); //If we defined a delay for this animation... if(this.animationDelay !== undefined && this.animationDelay[this.animationIndex] !== undefined) { //... set it delay = this.animationDelay[this.animationIndex]; $(this.htmlElement).css("animation-delay", delay).css("-webkit-animation-delay", delay).css("-moz-animation-delay", delay).css("-ms-animation-delay", delay).css("-o-animation-delay", delay); } //Increment animation counter this.animationIndex++; /* * Redraw element * This enables for two consecutive equal animations */ $(this.htmlElement).redraw(); //Wait for animation to finish, then call the callback function $(this.htmlElement).addClass(this.lastAnimation).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', callback); }; /**************************************************************************/ /** * Defines a group of Elements */ function Group() { /* * Constructor */ //This Group's Elements this.elements = []; //Flag to check if this element should only be animated when visible on screen this.animateWhenVisible = false; //Flag that indicates whether an animation is being performed this.isPerformingAnimation = false; //Flag that indicates if the animation should be reseted if the element goes off screen this.resetWhenOffscreen = false; //Flag that indicates if this group should loop indefinitely this.repeat = false; //Flag to check if this group is reset (this being in a clean, start from the beginning state) this.isReset = true; } /** * Resets the animation back to the beginning */ Group.prototype.reset = function() { //Check if we're not reset yet if(!this.isReset) { //Start iterating this group elements for(var i=1; i= topElementLimit) && (topWindowLimit <= bottomElementLimit)); }; /** * Check if it's time to animate any animate-only-when-visible group */ this.checkOnVisibleGroups = function() { //Iterate groups that should be animated when visible on screen for(var i=0; i