javascript - how to return animation to it's original size and position on a click -
i relatively new if see doing wrong, or anyways simplify code please not hesitate say.
i have following code enlarge div element:
var profilepostsclick = function () { $('.largebox, .smallbox, .longbox').click(function () { $(this).animate({ width: '100%', height: '40%' }, 200); $('.closepost', this).removeclass('closeposthide'); }); }; $(document).ready(profilepostsclick);
https://jsfiddle.net/jvkhmpbt/
i wanting close each div when cross clicked, returning it's original size , positioning (with height: auto if feasible).
aslo there way make each div opens above smaller ones? (like top left div does, aware because of it's positioning)
thanks
you save animation properties/values in cache-object , restore them after animation.
http://jsfiddle.net/jvkhmpbt/4/
var animationresetcache = []; var savevalues = function (node) { animationresetcache.push({ node: node, width: node.css('width'), height: node.css('height') }); }; var restorevalues = function (node) { (var = 0; < animationresetcache.length; ++i) { var item = animationresetcache[i]; if (item.node.is(node)) { return item; } } }; var profilepostsclick = function () { $('.largebox, .smallbox, .longbox').click(function (e) { var $this = $(this); if ($this.hasclass('open')) return; savevalues($this); $this.addclass('open').animate({ width: '100%', height: '40%' }, 200); $this.find('.closepost').removeclass('closeposthide'); }); $('.closepost').click(function () { var $parent = $(this).parent('.largebox, .smallbox, .longbox'); if ($parent.hasclass('open')) { var cachedvalues = restorevalues($parent); $parent.animate({ width: cachedvalues.width, height: cachedvalues.height }, function () { $parent.removeclass('open'); }); $parent.find('.closepost').addclass('closeposthide'); } }); }; $(document).ready(profilepostsclick);
Comments
Post a Comment