2010-08-10 87 views
0

我想创建带有变量'顶部'高度的slidingUp div,以便一个div可以slideUp 700px,而另一个div(内容较少)slidesUp只有400px。目前,divs都在同一高度上滑动。在CSS中没有div高度声明。我的脚本:jquery重置和设置变量

$(document).ready(function(){   
      var oldHeight; 
    $('a.menubtn').click(function(){    
      var newHeight; 
      if ($(this.id) == 'work') { 
        newHeight = 700; 
        } 
        else { if ($(this.id) == 'services') { 
         newHeight = 400; 
        } 
        else { if ($(this.id) == 'about') { 
         newHeight = 400; 
        } 
        else { 
         newHeight = 300; 
        } 
        } 
        } 

      if ($('.active').length > 0) { 

       $('.active').removeClass('active').animate({'top': '+=' + oldHeight + 'px'}, '200'); 
       $('div#contents_'+ this.id).addClass('active').animate({'top': '-=' + newHeight + 'px'}, '200'); 
       oldHeight = newHeight; 
       } 
       else 
    $('div#contents_'+ this.id).addClass('active').animate({'top': '-=' + newHeight + 'px'}, '200'); 
       oldHeight = newHeight; 
       } 
    return(false); 
    });    
}) 

TIA。

回答

2

您的if声明是错误的。

此:

$(this.id) == 'work' // here you're comparing a jQuery object to 'work' 

应该是:

this.id == 'work'  // here you're comparing the actual ID value to 'work' 

,如果你改变了if语句的结构是这样它会更清洁:

if (this.id == 'work') { 
    newHeight = 700; 
} else if (this.id == 'services') { 
    newHeight = 400; 
} else if (this.id == 'about') { 
    newHeight = 400; 
} else { 
    newHeight = 300; 
} 
+0

唉,有什么一个愚蠢的错误!谢谢:-) – circey 2010-08-10 23:11:56

+0

@circey - 不客气。 :O) – user113716 2010-08-10 23:18:33