jquery重置和设置变量
我试图创建具有可变“顶部”高度的slideUp div,这样一个div可以滑动700px,而另一个(内容较少)只能滑动400px。目前,div都在同一高度滑动。css中没有div高度声明。我的剧本:jquery重置和设置变量,jquery,variables,Jquery,Variables,我试图创建具有可变“顶部”高度的slideUp div,这样一个div可以滑动700px,而另一个(内容较少)只能滑动400px。目前,div都在同一高度滑动。css中没有div高度声明。我的剧本: $(document).ready(function(){ var oldHeight; $('a.menubtn').click(function(){ var newHeight;
$(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.如果您的
陈述有误,请告知您的
这:
应该是:
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;
}
if (this.id == 'work') {
newHeight = 700;
} else if (this.id == 'services') {
newHeight = 400;
} else if (this.id == 'about') {
newHeight = 400;
} else {
newHeight = 300;
}