effect-bounce.js (2608B)
1 /*! 2 * jQuery UI Effects Bounce 1.12.1 3 * http://jqueryui.com 4 * 5 * Copyright jQuery Foundation and other contributors 6 * Released under the MIT license. 7 * http://jquery.org/license 8 */ 9 10 //>>label: Bounce Effect 11 //>>group: Effects 12 //>>description: Bounces an element horizontally or vertically n times. 13 //>>docs: http://api.jqueryui.com/bounce-effect/ 14 //>>demos: http://jqueryui.com/effect/ 15 16 ( function( factory ) { 17 if ( typeof define === "function" && define.amd ) { 18 19 // AMD. Register as an anonymous module. 20 define( [ 21 "jquery", 22 "./effect" 23 ], factory ); 24 } else { 25 26 // Browser globals 27 factory( jQuery ); 28 } 29 }( function( $ ) { 30 31 return $.effects.define( "bounce", function( options, done ) { 32 var upAnim, downAnim, refValue, 33 element = $( this ), 34 35 // Defaults: 36 mode = options.mode, 37 hide = mode === "hide", 38 show = mode === "show", 39 direction = options.direction || "up", 40 distance = options.distance, 41 times = options.times || 5, 42 43 // Number of internal animations 44 anims = times * 2 + ( show || hide ? 1 : 0 ), 45 speed = options.duration / anims, 46 easing = options.easing, 47 48 // Utility: 49 ref = ( direction === "up" || direction === "down" ) ? "top" : "left", 50 motion = ( direction === "up" || direction === "left" ), 51 i = 0, 52 53 queuelen = element.queue().length; 54 55 $.effects.createPlaceholder( element ); 56 57 refValue = element.css( ref ); 58 59 // Default distance for the BIGGEST bounce is the outer Distance / 3 60 if ( !distance ) { 61 distance = element[ ref === "top" ? "outerHeight" : "outerWidth" ]() / 3; 62 } 63 64 if ( show ) { 65 downAnim = { opacity: 1 }; 66 downAnim[ ref ] = refValue; 67 68 // If we are showing, force opacity 0 and set the initial position 69 // then do the "first" animation 70 element 71 .css( "opacity", 0 ) 72 .css( ref, motion ? -distance * 2 : distance * 2 ) 73 .animate( downAnim, speed, easing ); 74 } 75 76 // Start at the smallest distance if we are hiding 77 if ( hide ) { 78 distance = distance / Math.pow( 2, times - 1 ); 79 } 80 81 downAnim = {}; 82 downAnim[ ref ] = refValue; 83 84 // Bounces up/down/left/right then back to 0 -- times * 2 animations happen here 85 for ( ; i < times; i++ ) { 86 upAnim = {}; 87 upAnim[ ref ] = ( motion ? "-=" : "+=" ) + distance; 88 89 element 90 .animate( upAnim, speed, easing ) 91 .animate( downAnim, speed, easing ); 92 93 distance = hide ? distance * 2 : distance / 2; 94 } 95 96 // Last Bounce when Hiding 97 if ( hide ) { 98 upAnim = { opacity: 0 }; 99 upAnim[ ref ] = ( motion ? "-=" : "+=" ) + distance; 100 101 element.animate( upAnim, speed, easing ); 102 } 103 104 element.queue( done ); 105 106 $.effects.unshift( element, queuelen, anims + 1 ); 107 } ); 108 109 } ) );