私は特定のアンカーリンクにGoogleのスムーズなスクロールスクリプトを使用していますが、それだけ#tothetop
ではありません#
。私はexample.jsファイル(スクリプトでダウンロードする)でこれを行いました:
かわった:
$('a[href*=#]').click(function() { ... });
に:
$('a[href*=#tothetop]').click(function() { ... });
だから今はスムーズなスクロールをにのみ適用し#tothetop
ますが、他の異なるアンカーリンクにどのように適用しますか?例:#tothetop
、、、#tothebottom
など#gotothepub
?(これは長い話ですが、要するに、ページ上の別のスクリプトと競合するため、なぜ「#」を使用しないのか尋ねないでください)私は試しました:
$('a[href*=#tothetop]','a[href*=#tothebottom]').click(function() { ... });
しかし、これは機能しません。私はJavascriptとPHPにあまり精通していませんが、これを行う簡単な方法があると確信していますか?
example.jsの完全なコードは次のようになります。
$(function(){
$('a[href*=#tothetop]').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}, 1000);
return false;
}
}
});
});