Extend $.animate() to detect CSS transitions for Webkit, Mozilla and Opera and convert animations automatically. Compatible with IE6+
Tested with jQuery 1.3.2 to 1.6.1
Properties supported: (more to come)
- left : using translate(x, y) or translate3d(x, y, z)
- top : using translate(x, y) or translate3d(x, y, z)
Note that any properties not mentioned above will simply be handled by the standard jQuery $.animate() method. This plugin adds new functionality without taking any away.
The idea is to simply enhance existing animations with a lightweight 'drop-in' plugin. Those looking for features such as IE transformations & translate() functionality should consider http://plugins.jquery.com/project/2d-transform
Simple animation demo and documentation found here: http://playground.benbarnett.net/jquery-animate-enhanced/
What it does
The plugin will analyse the properties you're animating on, and select the most appropriate method for the browser in use. This means your transitions on left, top and opacity will convert to a CSS3 transition on Webkit & Mozilla agents that support it, and Opera 10.50+. If the user is on a browser that has no CSS3 transitions, this plugin knows about it and won't get involved. Silent degradation :)
Multiple callback mechanisms are created internally to monitor for DOM manipulation and for all 'transitionend' CSS3 events to be picked up. This means you have one neat callback() for the whole animation regardless on whether the plugin is using CSS3, DOM, or both for its animations.
As this plugin uses CSS3 translate where available, there is an internal callback mechanism to reset the 'left' and/or 'top' properties so that your layout is unaffected.
Usage is identical to the jQuery animate() function, but it comes with 3 new paramaters, which are totally optional and safe to leave untouched for general use:
- avoidTransforms: (Boolean) By default the plugin will convert left and top animations to the CSS3 style -webkit-transform (or equivalent) to aid hardware acceleration. This functionality can be disabled by setting this to true.
- useTranslate3d: (Boolean) By default the plugin will use 2d translations due to wider browser support. Set this to true to use translate3d instead. Recommended for iPhone/iPad development (here's why).
- leaveTransforms: (Boolean) By default if the plugin is animating a left or a top property, the translate (2d or 3d depending on setting above) CSS3 transformation will be used. To preserve other layout dependencies, once the transition is complete, these transitions are removed and converted back to the real left and top values. Set this to true to skip this functionality.
Since v0.77, the plugin will now automatically use 3D Translations where supported. To override this functionality, you can either use the parameter above, or run $.toggle3DByDefault();
- Merge Pull request #57 from Kronuz
- Codebase cleaned and now passes jshint.
- Fixed a few bugs (it now saves and restores the original css transition properties).
- fadeOut() is fixed, it wasn't restoring the opacity after hiding it.
- Issue #28 - Report $(el).is(':animated') fix
- Issue #42 - Right negative position animation: please see issue notes on Github
- Issue #18 - jQuery/$ reference joys
- Adding feature on Github issue #44 - Use 3D Transitions by default
- Fixing issue #37 - fixed stop() method (with gotoEnd == false)
- Fixing issue #35 to pass actual object back as context for callback
- Fixing issue #29 to play nice with 1.6
- Merged Pull Request #26: Fixed issue with fadeOut() / "hide" shortcut
- Merged Pull Request #23: Added Penner equation approximations from Matthew Lein's Ceaser, and added failsafe fallbacks
- Merged Pull Request #24: Changes translation object to integers instead of strings to fix relative values bug with leaveTransforms = true
- Merged Pull Request from amlw-nyt to add bottom/right handling
- Width/height fixes & queue issues resolved.
- Code cleanups & file size improvements for compression.
- Zero second fadeOut(), fadeIn() fixes
- Callbacks with queue() support refactored to support element arrays
- BUGFIX #13: .slideUp(), .slideToggle(), .slideDown() bugfixes in Webkit
- BUGFIX #11: callbacks not firing when new value == old value
- BUGFIX #11: queue is not a function issue fixed
- BUGFIX #10: Negative positions converting to positive
- Animate function rewrite in accordance with new queue system
- BUGFIX #8: Left/top position values always assumed relative rather than absolute
- BUGFIX #9: animation as last item in a chain — the chain is ignored?
- BUGFIX: width/height CSS3 transformation with left/top working
- isEmptyObject function for <jQuery 1.4 (plugin still requires 1.3.2)
- License changed to MIT (http://www.opensource.org/licenses/mit-license.php)
- Removed silly check for 'jQuery UI' bailouts. Sorry.
- Scoping issues fixed - Issue #4: $(this) should give you a reference to the selector being animated.. per jquery's core animation funciton.
- New $.translate() method to easily calculate current transformed translation
- Repeater callback bug fix for leaveTransforms:true (was constantly appending properties)
- leaveTransforms: true bug fixes
- 'Applying' user callback function to retain 'this' context
- Bailing out with jQuery UI. This is only so the plugin plays nice with others and is TEMPORARY.
- Support for $.fn.stop()
- Fewer jQuery.fn entries to preserve namespace
- All references $ converted to jQuery
- jsDoc Toolkit style commenting for docs (coming soon)
- Handling of 'undefined' errors for secondary CSS objects
- Support to enhance 'width' and 'height' properties (except shortcuts involving jQuery.fx.step, e.g slideToggle)
- Bugfix: Positioning when using avoidTransforms: true (thanks Ralf Santbergen reports)
- Bugfix: Callbacks and Scope issues
- Checks for 3d support before applying
- Compatible with .fadeIn(), .fadeOut()
- Use shortcuts, no duration for jQuery default or "fast" and "slow"
- Clean up callback event listeners on complete (preventing multiple callbacks)
- Compatible with .slideUp(), .slideDown(), .slideToggle()
- 'Zero' position bug fix (was originally translating by 0 zero pixels, i.e. no movement)
- Iterate over multiple elements and store transforms in jQuery.data per element
- Include support for relative values (+= / -=)
- Better unit sanitization
- Performance tweaks
- Fix for optional callback function (was required)
- Applies data[translateX] and data[translateY] to elements for easy access
- Added 'easeInOutQuint' easing function for CSS transitions (requires jQuery UI for JS anims)
- Less need for leaveTransforms = true due to better position detections