A solution is available, with a practical demonstration provided below along with detailed explanations. Please pay close attention to the important caveats mentioned.
Test the browser's ability to animate between two values using the code snippet below.
The Code Snippet
View the jsFiddle demo here.
/*
@param property Property being tested.
@param from Initial starting value for the animation.
@param to Ending value for the animation.
@param [element] Element used for testing (required for certain properties).
*/
function isAnimationSupported(property, from, to, element) {
// Function logic goes here
}
...
How to Utilize
Ensure you provide the necessary arguments - the property to be animated and its start/end values. Optionally, include an element with pre-set styles like position: absolute
. If no element is passed, default styles are applied to a test div
.
Functionality Overview
This script adds a keyframe animation rule to a temporary stylesheet, animating an element from the initial frame to the final frame as defined by the input values. The computed style of the animated property is then compared between the initial and final frames to determine animatability.
It works because both transitions and keyframe animations utilize the same set of animatable properties, ensuring compatibility across browsers.
Important Considerations
Browsers handle animations inconsistently, with some issues that have been mitigated in the script but others remain unavoidable.
For instance, Firefox tweens position values like left
on statically positioned elements, unlike Webkit and Opera. This can result in varying behavior when animating position values without specifying a non-static positioning for the element.
Major modern browsers supporting CSS transitions also typically support CSS keyframes, though older versions may lack keyframe support (e.g., Opera 11).
Using a tool like prefixfree for determining correct prefixes directly would enhance elegance; currently, an array of prefixes is tested beginning with the unprefixed version.