Last active
August 29, 2015 14:14
-
-
Save eatonphil/e4a81aa10f4364629384 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Original Source: http://darcyclarke.me/development/fix-jquerys-animate-to-allow-auto-values-2/ | |
* | |
* This is an addition to Darcy Clark's animateAuto function that allows you to animate a div with | |
* a set max-width/height growing to full height/width. In my use case, I had a div with a height | |
* larger than its default max-height. I wanted to animate the growth from the set max-height to the | |
* actual height. This addition to Darcy's function simply turns off the max-height/width and immediately | |
* sets the actual height of the div to the previous max-height/width. Then it animates to auto like normal. | |
* Also, the speed variable can optionally be a function of height or width or both. | |
*/ | |
jQuery.fn.animateAuto = function(prop, speed, callback){ | |
var elem, height, width; | |
return this.each(function(i, el){ | |
var el = jQuery(el), | |
height0 = el.height()+"px", | |
width0 = el.width()+"px", | |
elem = el.clone().css({"height":"auto","width":"auto"}).appendTo("body"), | |
height = elem.css("height"), | |
width = elem.css("width"); | |
el.css({"max-height": "none", "max-width": "none", "height": height0, "width": width0}); | |
elem.remove(); | |
var _speed = function() {return speed;}; | |
if (isFunction(speed)) _speed = function(x) {return speed(x);}; | |
if(prop === "height") | |
el.animate({"height":height}, _speed(height), callback); | |
else if(prop === "width") | |
el.animate({"width":width}, _speed(width), callback); | |
else if(prop === "both") | |
el.animate({"width":width,"height":height}, _speed(width, height), callback); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment