2015-04-07 45 views
0

之後觸發ScrollTo這個想法是點擊一個鏈接轉到另一個頁面(index.html),一旦你在那裏,立即轉到一個錨點div。我試圖用觸發事件做到這一點,但不工作。 scrollTo在index.html中工作,但我不能從反向鏈接自動執行。轉到另一個頁面並在

這裏是我的代碼:

$(".goTo").click(function() { 
    /* If I'm in the index, it's working fine */ 
    if($('.show').parents('html').length > 0){ 
     $('body').animate({ 
      scrollTop: $(".works").offset().top 
     }, 1500, 'easeInOutExpo'); 
    }else{ 
     location.href = "index.html"; 
     $(".goTo").trigger('click'); 
    } 
}); 

在此先感謝。

+0

切換到另一個頁面後,舊頁面中的腳本停止運行。 – Barmar

+0

頁面刷新後狀態丟失。你必須通過queryparams來完成。 – mohamedrias

+3

你可以使用'location.href =「index.html#id」;'並且它將滾動到帶有該ID的DIV。 – Barmar

回答

1

你應該在你的href位置使用錨這樣的:location.href = "index.html#id";

,然後得到你想要的id滾動到這樣的:var hash = window.location.hash.substring(1);

因此,如何你的代碼看起來:

$(".goTo").click(function() { 
      window.location.href = "index.html#id"; 
     } 
}); 

// Shorthand for document ready 
$(function() { 
// This will get you everything behind the anchor !! 
var hash = window.location.hash.substring(1); 
var tag = $("#"+hash+""); 
// Animation 
$('html,body').animate({scrollTop: tag.offset().top},'slow'); 
}); 

未經測試,並不確定這是否適用於您,但至少可以告訴您操作過程。

+0

所有我不得不改變的是動畫的效果,但其他作品完美無缺。非常感謝您的參與!! – Antonio

相關問題