javascript - jQuery Simpler Way to Do True Slideup/Down -


i'm trying true sliding jquery (so element slides up), came following, i'm not sure it's optimised, can see way improve it?

fiddle at: http://jsfiddle.net/gpuhg/1/

the idea .content elements default being height off page or behind header, , when item on menu clicked if windows open close , requested item slides down. solution seems rather bloated simple!

jquery:

$.fn.exists = function() {     return this.length !== 0; }  $(".content").each(function() {     $(this).hide().css({         "margin-top": "-=" + $(this).outerheight() + "px"     }); });  $("#navigation ul li").each(function() {     var relatedcontent = $("#" + $(this).attr("title") + "-content");     $(this).click(function() {         if (!$(":animated").exists()) {             if ($(".open").exists()) {                 $(".current").first().removeclass("current");                 $(this).addclass("current");                 var element = $(".open").first();                 element.removeclass("open").animate({                     "margin-top": "-" + element.outerheight() + "px"                 }, 500, function() {                     $(this).hide();                     relatedcontent.show().addclass("open").animate({                         "margin-top": "0px"                     }, 500);                 });             } else {                 $(this).addclass("current");                 relatedcontent.show().addclass("open").animate({                     "margin-top": "0px"                 }, 500);             }         }     }); });​ 

thanks time,

overall, code fine, didn't change much. remember cache variables as possible; dom access expensive. also, providing context selectors improve performance. there many more tweaks make this, here few:

var $nav = $('#navigation'); $nav.find("ul li").click(function(){     if($(':animated').length) { return; }     var $this = $(this),         relatedcontent = $("#"+$this.attr("title")+"-content", $nav);     if(!relatedcontent.length) { return; }     if($(".open", $nav).length) {         $(".current", $nav).removeclass("current");         $this.addclass("current");         var element = $(".open", $nav).first();         element.removeclass("open").animate({"margin-top": "-"+element.outerheight()+"px"},             500,             function() {                 $(this).hide();                 relatedcontent.show().addclass("open").animate({"margin-top": "0px"},500);             });     } else {         $this.addclass("current");         relatedcontent.show().addclass("open").animate({"margin-top": "0px"},500);      } });​ 

edit: agree comment above: $.fn.exists function great convenience, using .length shorter , faster hitting additional function. keep if like, i'm ditching in code.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -