【发布时间】:2012-09-03 08:12:30
【问题描述】:
正如标题所说,我认为这段代码可以用更少的行来简化。有人可以帮我解决这个问题吗?
$('a[href=#over]').click(function(){
$('html, body').animate({
scrollTop: $("#over").offset().top - 100
}, 2000);
});
$('a[href=#diensten]').click(function(){
$('html, body').animate({
scrollTop: $("#diensten").offset().top - 100
}, 2000);
});
$('a[href=#portfolio]').click(function(){
$('html, body').animate({
scrollTop: $("#portfolio").offset().top - 100
}, 2000);
});
$('a[href=#contact]').click(function(){
$("html, body").animate({ scrollTop: $(document).height() }, 2000);
});
$('a[href=#top]').click(function(){
$('html, body').animate({scrollTop:0}, 2000);
return false;
});
我在想自己不要使用 if/elseif 语句,但我有点卡在那里。那你能看看吗?
【问题讨论】:
-
您尝试过简化它吗?
-
作为提示,对于前三个,您使用的选择器等于元素上
href属性的值。 -
是的,我试过了,但由于 #top 和 #contact 与其他 3 个不同,我被卡住了。
标签: javascript jquery jquery-animate