Here I use the caching of the selected selectors (and the obvious closing area, so I put $(window).load(); ) there, you will have better overall performance.
You need to cache if you are animating. You must. Do not animate over and over when re-selecting a jQuery object call, such as $('.map_wrapper').stop().animate() . It is inefficient and should be executed only when necessary, as when changing the DOM and it is necessary to find the current state.
My approach simply switches the effect variable with the new settings if there is a .toggle on the button. Simple, reliable. There seems to be a slight lag since the iframe loading from Google Maps. This may be unavoidable in jsFiddle environment.
$(window).load(function(){ var $mapbtn = $('.map_btn'), $wrapper = $('.map_wrapper'); $mapbtn.click(function() { var $this = $(this), effect = { opacity: 0, height: 0 }; $this.toggleClass('toggle'); if ($this.is('.toggle')) { effect.opacity = 1; effect.height = 420; } $wrapper.stop().animate(effect); }); });
http://jsfiddle.net/userdude/h2fh6/21/
source share