Animacija višine / širine na "Samodejno" - CSS-triki

Anonim

To ni mogoče storiti thing.animate(( "height": "auto" ));. Torej je to metoda Darcy Clarke, ki omogoča, da to deluje. V bistvu klonirate element, odstranite fiksne višine, ki trenutno vplivajo na element, in izmerite / shranite vrednost. Nato realni element animirate do te vrednosti.

jQuery.fn.animateAuto = function(prop, speed, callback)( var elem, height, width; return this.each(function(i, el)( el = jQuery(el), elem = el.clone().css(("height":"auto","width":"auto")).appendTo("body"); height = elem.css("height"), width = elem.css("width"), elem.remove(); if(prop === "height") el.animate(("height":height), speed, callback); else if(prop === "width") el.animate(("width":width), speed, callback); else if(prop === "both") el.animate(("width":width,"height":height), speed, callback); )); )

Uporaba

$(".animateHeight").bind("click", function(e)( $(".test").animateAuto("height", 1000); )); $(".animateWidth").bind("click", function(e)( $(".test").animateAuto("width", 1000); )); $(".animateBoth").bind("click", function(e)( $(".test").animateAuto("both", 1000); ));