我有一個onepager網站,我使用scrollmagic加上所有必要的插件/庫(和jQuery),用於動畫,釘住,淡化過程等不同效果。由滾動位置觸發。動畫滾動腳本阻止從外部鏈接訪問本地錨位置
我還將它用於動畫滾動到頁面上的定位點(從菜單和其他本地鏈接) - 請參閱以下腳本的相應部分。
的問題是,這個腳本抑制的「跳躍」直接到anchorpoint默認行爲單擊本地連接時,顯然也當頁面從外部通過直接鏈接或書籤與錨訪問附加到URL(如http://www.example.com/index.php#part3
)。儘管單擊本地鏈接時需要此行爲,但顯然會阻止瀏覽器在其他位置鏈接錨點時顯示錨點位置。
有沒有什麼辦法可以讓瀏覽器直接顯示錨定位置,當點擊上面例子中的鏈接時?
var sm_controller_1 = new ScrollMagic.Controller();
sm_controller_1.scrollTo(function(anchor_id) {
TweenMax.to(window, 2.0, {
scrollTo: {
y: anchor_id
autoKill: true
},
ease: Cubic.easeInOut
});
});
jQuery(document).on("click", "a[href^=#]", function(e) {
var id = jQuery(this).attr('href');
if(jQuery(id).length > 0) {
e.preventDefault();
sm_controller_1.scrollTo(id);
if (window.history && window.history.pushState) {
history.pushState("", document.title, id);
}
}
});
(由於問題在於從外部源調用原始URL,所以創建小提琴/ codepen沒有任何意義)。
是的,這個工程!我只是使用了你發佈的代碼,而不是下面的建議,因爲它已經按我想要的方式工作(即當頁面加載URL中的錨點時直接跳到錨點)。非常感謝你! – Johannes