2013-03-25 126 views
0

如何使用JavaScript爲Tumblr主題創建箭頭鍵導航?使用JavaScript的Tumblr箭頭鍵導航

我正在嘗試編寫一些JavaScript以允許Tumblr主題的用戶使用leftright箭頭鍵來導航頁面。

我已經成功地獲得基本的概念工作:

document.onkeydown = function(event) { 
    event = event || window.event; 
    var key = event.keyCode; 
    var currentPage = {CurrentPage}; 
    // left arrow key 
    if (key == 37) { 
     var previousPage = currentPage - 1; 
     window.location.href = "/page/" + previousPage; 
    } 
    // right arrow key 
    if (key == 39) { 
     var nextPage = currentPage + 1; 
     window.location.href = "/page/" + nextPage; 
    } 
}; 

這跑進用戶能夠超越最後一頁,可能(我不是很確定)的永久工作的問題頁面。但是,爲網址使用{PreviousPage}{NextPage}變量要容易得多,但由於某些原因,我無法讓它們在JavaScript中工作。即使我用JS(即{JSPreviousPage}{JSNextPage})作爲它們的前綴,它們仍然毫無結果,儘管HTML中的變量工作正常。

回答

2

將下一個和prev ID添加到您的分頁錨點鏈接。然後試試這個:

$(document).keydown(function(e) { 
var url = false; 
    // Left arrow key code 
    if (e.which == 37) { 
     url = $('#prev').attr('href'); 
    } 
    // Right arrow key code 
    else if (e.which == 39) { 
     url = $('#next').attr('href'); 
    } 

    if (url) { 
     window.location = url; 
    } 

});

+0

謝謝。完全超過了這個想法。 – Spencer 2013-03-27 03:50:37