2016-11-29 77 views
1

我正在使用fullPage.js插件逐頁滾動。該插件使用哈希URL來完成。在頁面刷新時,文檔始終在最後一個哈希鏈接上加載。如何在每次刷新後將文檔加載到頂部?我嘗試添加以下代碼,但沒有奏效:每次刷新後在index.html加載頁面

$(document).ready(function(){ 
    $(document).scrollTop(0); 
}); 

這是鏈接到的網頁 - https://rimildeyjsr.github.io/St.Anthony-Website

的jQuery:

$('#fullpage #section1').hide(); 
$(document).ready(function(){ 
     $('#fullpage #section1').show(); 
     $('#fullpage').fullpage({ 
      anchors:['Page1','Page2','lastpage'], 
      afterLoad : function(anchorLink,index) { 
       if (index == 2 || anchorLink == 'Page2'){ 
        $('.school-name').addClass('animated slideInUp').css('visibility','visible'); 
       } 
      } 

     }); 

鏈接的github倉庫:https://github.com/rimildeyjsr/St.Anthony-Website

+0

你是否試圖定期刷新頁面? – xtechkid

+0

我一直沒有用過哈希,但問題是,如果你堅持在控制檯中,這是否導致頁面跳回到頂部? 'window.location.hash ='';' – Taplar

+0

@xtechkid:不,我只是想在每次刷新後在最上面加載頁面。這就是全部 –

回答

2

它看起來像這樣在你的頁面上導致它回到頂部。

window.location.hash = '#Page1'; 

所以,你可以嘗試這樣做,

$(window).on('load', function() { 
    window.location.hash = '#Page1'; 
}); 

應該發生的任何一次加載的頁面,其中包括令人耳目一新。

1

試試這個

if(location.href.split('#').length > 0){ 
    location.href = location.href.split('#')[0] 
} 

讓我知道如果你面臨任何問題

0
if (location.hash) { 
    setTimeout(function() { 

    window.scrollTo(0, 0); 
    }, 1); 
} 
1

正確的方法是通過使用fullPage.js功能moveTo

$(window).on('load', function() { 
    $.fn.fullpage.moveTo(1); 
}); 

而是通過改變位置哈希建議的解決方案也將這樣做。

相關問題