Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding keyframe animation support #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions jquery.transit.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
support.transformOrigin = getVendorPropertyName('transformOrigin');
support.transform3d = checkTransform3dSupport();

// Check for browser's animation support.
support.animation = getVendorPropertyName('animation');
support.animationPlayState = getVendorPropertyName('animationPlayState');

var eventNames = {
'transition': 'transitionEnd',
'MozTransition': 'transitionend',
Expand All @@ -79,6 +83,17 @@
// Detect the 'transitionend' event needed.
var transitionEnd = support.transitionEnd = eventNames[support.transition] || null;

var animationEndEvents = {
'animation': 'animationend',
'MozAnimation': 'animationend',
'oAnimation': 'oanimationend',
'WebkitAnimation': 'webkitAnimationEnd',
'msAnimation': 'MSAnimationEnd'
}

// Detect the 'animationend' event needed.
var animationEnd = support.animationEnd = animationEndEvents[support.animation] || null;

// Populate jQuery's `$.support` with the vendor prefixes we know.
// As per [jQuery's cssHooks documentation](http://api.jquery.com/jQuery.cssHooks/),
// we set $.support.transition to a string of the actual property name used.
Expand Down Expand Up @@ -200,6 +215,34 @@
elem.style[support.transition] = value;
}
};

// ## 'animation' CSS hook
// Allows you to use the `animation` property in CSS.
//
// $("#hello").css({ animation: 'bounce-in 1s ease-in' });
//
$.cssHooks.animation = {
get: function(elem) {
return elem.style[support.animation];
},
set: function(elem, value) {
elem.style[support.animation] = value;
}
};

// ## 'animationPlayState CSS hook
// Allows you to use the `animationPlayState` property in CSS.
//
// $("#hello").css({ animationPlayState: 'paused' });
//
$.cssHooks.animationPlayState = {
get: function(elem) {
return elem.style[support.animationPlayState];
},
set: function(elem, value) {
elem.style[support.animationPlayState] = value;
}
};
}

// ## Other CSS hooks
Expand Down Expand Up @@ -631,6 +674,122 @@
return this;
};

// ## $.fn.playKeyframe
// Play a CSS3 keyframe-based animation. Only the name property is mandatory, everything else
// is optional and will default to reasonable values.
//
// $("...").playKeyframe({ name: 'bounce-in' });
//
// // Specific duration
// $("...").playKeyframe({ name: 'bounce-in', duration: 1000 });
//
// // With all settings
// $("...").playKeyframe({ name: 'bounce-in', duration: 1000,
// delay: 2000, repeat: 2, easing: 'ease-in' });
//
// // With callback
// $("...").playKeyframe({ name: 'bounce-in' }, function() { ... });
//
// // Alternate syntax
// $("...").playKeyframe({
// name: 'bounce-in'
// duration: 1000,
// delay: 2000,
// repeat: 2,
// easing: 'ease-in',
// complete: function() { /* ... */ }
// });
//
$.fn.playKeyframe = function(properties, callback) {

var self = this;

// Set some default properties.
properties = $.extend({
duration: $.fx.speeds._default,
easing: $.cssEase._default,
delay: 0,
repeat: 1, // May be an int or 'infinite'
direction: 'normal',
fillMode: 'none'
}, properties || {});

if (!properties.name) throw("Name required")

// Alternate syntax.
if (typeof properties.complete !== 'undefined') {
callback = properties.complete;
delete properties.complete;
}

properties.duration = toMS(properties.duration);
properties.delay = toMS(properties.delay);

// Build the CSS line
var animationCSS = [properties.name, properties.duration, properties.easing, properties.delay,
properties.repeat, properties.direction, properties.fillMode].join(' ');

// Prepare the callback
var cb = function() {
if (typeof callback === 'function') { callback.apply(self); }
self.data('keyframe', false);
self.css({animationPlayState: '', animation: ''});
};

// We'll assume animationEnd is available since it should be on all browsers
// which support keyframe animations.
self.one(support.animationEnd, cb);

self.css({animationPlayState: 'running'});
self.data('keyframe', properties.name);
self.css({animation: animationCSS})

// Chainability.
return this;
}

// ### pauseKeyframe()
// Pauses a currently running keyframe animation.
//
// Example:
//
// $("#foo").pauseKeyframe();
//
$.fn.pauseKeyframe = function() {
this.css({animationPlayState: 'paused'});

// Chainability.
return this;
}

// ### resumeKeyframe()
// Resumes a currently paused keyframe animation.
//
// Example:
//
// $("#foo").resumeKeyframe();
//
$.fn.resumeKeyframe = function() {
this.css({animationPlayState: 'running'});

// Chainability.
return this;
}

// ### resetKeyframe()
// Stops and removes any keyframe animations from the specified element. This will also
// clear any callbacks presently waiting for the animation to end.
//
// Example:
//
// $("#foo").resetKeyframe();
//
$.fn.resetKeyframe = function() {
this.css({animationPlayState: 'running', animation: 'none'});
this.data('keyframe', false);
this.unbind(support.animationEnd);
}

function registerCssHook(prop, isPixels) {
// For certain properties, the 'px' should not be implied.
if (!isPixels) { $.cssNumber[prop] = true; }
Expand Down