这应该是一个非常基本的问题,但是我早上大部分时间都在扔它,而且此时我已经接近了.我甚至没有一点js foo - 但我找到了一个很好的评论代码块,我希望用它来动画锚链接:
$(document).ready(function() {
$('a[href*=#]').bind('click', function(e) {
e.preventDefault(); //prevent the "normal" behaviour which would be a "hard" jump
var target = $(this).attr("href"); //Get the target
var scrollToPosition = $(target).offset().top;
// perform animated scrolling by getting top-position of target-element and set it as scroll target
$('html, body').stop().animate({ scrollTop: scrollToPosition}, 600, function() {
location.hash = target; //attach the hash (#jumptarget) to the pageurl
});
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
我试图让它落在偏移量()上方30px.顶部 - 我试过了
$('html, body').stop().animate({ scrollTop: scrollToPosition -30}, 600,
这几乎是有效的 - 它去了正确的地方,然后反弹回来. …