Aug*_*gus -3 javascript jquery jquery-animate
正如标题所说,我认为这个代码可以在更少的行中简化.有人可以帮我吗?
$('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;
});
Run Code Online (Sandbox Code Playgroud)
我当时正在考虑if/elseif语句,但我有点卡在那里.你能看一下吗?
您可以使用href锚点的属性来按ID选择元素.
$('a').click(function(){
var id = this.href;
$('html, body').animate({
scrollTop: $(id).offset().top - 100
}, 2000);
});
Run Code Online (Sandbox Code Playgroud)
$('a').click(function(e) {
e.preventDefault();
var id = this.href;
var scroll = '';
if (id === '#contact') {
scroll = $(document).height();
} else if (id === '#top') {
scroll = 0;
} else {
scroll = $(id).offset().top - 100;
}
$('html, body').animate({
scrollTop: scroll
}, 2000)
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
106 次 |
| 最近记录: |