jquery.cssanimateauto
Version:
A jQuery plugin that uses CSS transitions to animate an element's height or width to or from auto.
6 lines • 2.55 kB
JavaScript
/*
* jquery.cssAnimateAuto - https://github.com/davidtheclark/jquery.cssAnimateAuto
* Copyright 2015, David Clark
* Released under the MIT license, 2015-08-04T19:24:35
*/
!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof module&&module.exports?module.exports=a(require("jquery")):a(jQuery)}(function(a){function b(b,c,d){function e(a){return a.hasClass(m.openClass)||a.css(n)===h(a)}function f(a){var b=a||"";return b+"."+m.eventNamespace}function g(a,b){function c(){a.css("transition",""),b(),d.call(a),a.off(f()).data("transitioning",!1)}function e(){a.data("transitioning")||a.trigger(f("fallback"))}var g=1e3*(parseFloat(a.css("transition-duration"))+parseFloat(a.css("transition-delay")))+300;a.css("transition",m.transition).on(f(p),function(a){a.originalEvent.propertyName===n&&c()}).on(f("fallback"),c).data("transitioning",!0),setTimeout(e,g)}function h(a){var b=a.clone().css({oppositeDimension:a.css(o),visibility:"hidden"}).appendTo(a.parent()),c=b.css(n,"auto").css(n);return b.remove(),c}function i(a){g(a,function(b){a.css(n,"auto"),a.addClass(m.openClass)}),a.css(n,h(a))}function j(a){var b=m.to?m.to:"";a.css(n,a.css(n)),a[0].offsetHeight,g(a,function(b){a.removeClass(m.openClass)}),a.css(n,b)}function k(a){e(a)?j(a):i(a)}var l=a(b),m=a.extend({},a.fn.cssAnimateAuto.defaults,c),n=m.transition.split(" ")[0],o="height"===n?"width":"height",p="undefined"!=typeof document.body.style.webkitTransition?"webkitTransitionEnd":"transitionend";switch(m.action){case"open":e(l)||i(l);break;case"close":e(l)&&j(l);break;case"toggle":k(l);break;default:throw new Error('jquery.cssAnimateAuto only performs the actions "open", "close" and "toggle". You seem to have tried something else.')}}function c(){for(var b={},c=function(){},d=arguments.length,e=["open","close","toggle"],f=["height","width"],g=0;d>g;g++){var h=arguments[g],i=typeof h;if(h)switch(i){case"string":if(-1!==e.indexOf(h))a.extend(b,{action:h});else if("to"===h.substring(0,2))a.extend(b,{to:h.substring(3)});else{if(-1===f.indexOf(h.split(" ")[0]))throw new Error("jquery.cssAnimateAuto doesn't know what to do with your argument \""+h+'"');a.extend(b,{transition:h})}continue;case"function":c=h;continue;case"object":a.extend(b,h);continue}}return[b,c]}a.fn.cssAnimateAuto=function(){var d=c.apply(this,arguments);return this.each(function(){a(this).data("transitioning")||b.apply(null,[this].concat(d))})},a.fn.cssAnimateAuto.defaults={transition:"height 0.3s",action:"toggle",openClass:"is-opened",eventNamespace:"cssaa",to:!1}});