jquery-ui/ui/effects/effect-bounce.js

111 lines
2.6 KiB
JavaScript
Raw Normal View History

/*!
* jQuery UI Effects Bounce @VERSION
2012-07-04 13:08:08 +00:00
* http://jqueryui.com
*
* Copyright jQuery Foundation and other contributors
2012-08-09 14:13:24 +00:00
* Released under the MIT license.
* http://jquery.org/license
*/
//>>label: Bounce Effect
//>>group: Effects
//>>description: Bounces an element horizontally or vertically n times.
//>>docs: http://api.jqueryui.com/bounce-effect/
//>>demos: http://jqueryui.com/effect/
( function( factory ) {
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define( [
"jquery",
"../version",
"../effect"
], factory );
} else {
// Browser globals
factory( jQuery );
}
}( function( $ ) {
2008-05-23 09:26:18 +00:00
return $.effects.define( "bounce", function( options, done ) {
var upAnim, downAnim, refValue,
element = $( this ),
// Defaults:
mode = options.mode,
hide = mode === "hide",
show = mode === "show",
direction = options.direction || "up",
distance = options.distance,
times = options.times || 5,
// Number of internal animations
anims = times * 2 + ( show || hide ? 1 : 0 ),
speed = options.duration / anims,
easing = options.easing,
// Utility:
ref = ( direction === "up" || direction === "down" ) ? "top" : "left",
2012-04-19 01:57:51 +00:00
motion = ( direction === "up" || direction === "left" ),
i = 0,
queuelen = element.queue().length;
$.effects.createPlaceholder( element );
refValue = element.css( ref );
// Default distance for the BIGGEST bounce is the outer Distance / 3
if ( !distance ) {
distance = element[ ref === "top" ? "outerHeight" : "outerWidth" ]() / 3;
}
if ( show ) {
downAnim = { opacity: 1 };
downAnim[ ref ] = refValue;
2011-03-07 00:48:14 +00:00
// If we are showing, force opacity 0 and set the initial position
// then do the "first" animation
element
.css( "opacity", 0 )
2011-06-21 06:15:42 +00:00
.css( ref, motion ? -distance * 2 : distance * 2 )
.animate( downAnim, speed, easing );
}
// Start at the smallest distance if we are hiding
if ( hide ) {
distance = distance / Math.pow( 2, times - 1 );
}
downAnim = {};
downAnim[ ref ] = refValue;
// Bounces up/down/left/right then back to 0 -- times * 2 animations happen here
for ( ; i < times; i++ ) {
upAnim = {};
upAnim[ ref ] = ( motion ? "-=" : "+=" ) + distance;
element
.animate( upAnim, speed, easing )
.animate( downAnim, speed, easing );
distance = hide ? distance * 2 : distance / 2;
}
// Last Bounce when Hiding
if ( hide ) {
upAnim = { opacity: 0 };
upAnim[ ref ] = ( motion ? "-=" : "+=" ) + distance;
element.animate( upAnim, speed, easing );
}
2012-04-19 01:57:51 +00:00
element.queue( done );
$.effects.unshift( element, queuelen, anims + 1 );
} );
2008-05-23 09:26:18 +00:00
} ) );