これはかなり基本的な質問になるはずですが、私は朝のほとんどをそれに投げかけました、そしてこの時点で私はタオルを投げるところです。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;
});
});
私はそれをoffset()。topの30px上に着陸させようとしています-私は試しました
$('html, body').stop().animate({ scrollTop: scrollToPosition -30}, 600,
これはほとんど機能します-それは正しい場所に行きますが、その後跳ね返ります。
私も試しました
scrollTop: $(target).offset().top - 20 },
私も試しました
scrollTop: $(hash).offset().top + $('#access').outerHeight()
これは何も変わらないようです。
答えはここにあるようです:ヘッダーが修正されたJQueryページスクロールの問題 ですが、私はそれを完全に理解できないようです。
これは他の質問と似ていることはわかっていますが、見つけたものを確認しましたが、問題を解決するものをコピーして貼り付けることができなかったため、十分な知識がありません。
私は解決策に非常に感謝しています。
どうもありがとう、
マーティン
PS
私が見つけたこの他のコードのチャンクは機能しますが、ハッシュタグが削除されているため、ほとんど役に立ちません。
$(function(){
$('a[href*=#]').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
&& location.hostname == this.hostname) {
var $target = $(this.hash);
$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
if ($target.length) {
var targetOffset = $target.offset().top;
$('html,body').animate({scrollTop: targetOffset - 30}, 1000);
return false;
}
}
});
});