2012-11-27 65 views
1

這是iScroll的一個已知問題,它似乎只發生在iOS5中菜單完全停止工作。我在iScroll中的所有子鏈接都是散列錨。有沒有人有這方面的解決方法?iScroll網址哈希變化支持

回答

2

我處理它的方式是自己劫持錨鏈接,並用scrollToElement來代替它們。

// Hijack hash anchors and scroll to them 
$('a').click (function (e) { 
    var id = $(this).attr('href'); 
    if (id.substr(0,1) == '#') { 
     e.preventDefault(); 
     setTimeout(function() { 
      scroller.scrollToElement (id, 0); 
     }, 0); 
     return false; 
    } else { 
     return true; 
    } 
}); 

此代碼只能劫持以#開頭的鏈接。然後,它在setTimeout中處理scrollToElement,修復了其他一些間歇性錯誤。只要您的錨點正確地使用id命名,它就可以在我的最後工作。如果您使用name屬性而不是id屬性,則需要重寫這些屬性。

此代碼將複製name屬性,並將它們置於id屬性中(如果它爲空)。不過,你可能不需要這個。

$('a').each (function (i, e) { 
    var n = $(e).attr('name'); 
    var id = $(e).attr('id'); 
    if (typeof id == 'undefined' || id === null || id === '') { 
     $(e).attr('id', n); 
    } 
});