2014-04-26 40 views
0

BLUF:更新location.hash與FF一個滾動到的使用jQuery航點元素的ID引起滾動鼓泡/口吃,但不是Chrome或IE。火狐 - 更新window.location.hash上滾動與jQuery航點

問題: 我使用jQuery Waypoints通過一個表,這個基本的佈局做了幾件事情在用戶滾動:

____________________________________________________ 
| <tr class="category" id="cat1">Category One</tr> | 
| Cell   | Cell  | Cell   | 
| Cell   | Cell  | Cell   | 
| Cell   | Cell  | Cell   | 
____________________________________________________ 
| <tr class="category" id="cat2">Category Two</tr> | 
| Cell   | Cell  | Cell   | 
| Cell   | Cell  | Cell   | 
| Cell   | Cell  | Cell   | 

一個我想要做的事情是追加ID在視口的頂部window.location.category行。這個jQuery工作在Chrome 34和IE,但在FF 28它會導致在瀏覽器試圖跳回把元素在即使用戶不斷向下滾動視口頂部的「冒泡」的行爲。我在這裏註釋掉preventDefault因爲它雖然修復了FF跳,這也從到達第一個.category元素之後更新哈希停止腳本。有沒有人遇到與更新在FF location.hash或對如何解決它的想法類似的問題?

$('.category').waypoint(function() { 
    //strip current hash from window.location 
    window.location.hash.replace('#',''); 
    //store waypoint element's id 
    var setHref = $(this).attr('id'); 
    //set id as location.hash 
    window.location.hash = setHref; 
    //e.preventDefault(); 
    return false; 
}, { context: 'section' }); 

回答

0

這是更改location.hash的核心功能:它將您捲動到由散列引用的片段。沒有使用location.hash繞過它。

對於非常流行的瀏覽器,您可以使用pushStatereplaceState來操作URL和導航歷史記錄,而不會導致跳到新推送的文檔片段。