我對特定錨鏈接使用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;
}
}
});
});
應該還有jquery標籤嗎? –