keyboard navigation with arrow keys (next and back) for changing filenames

耗尽温柔 提交于 2019-12-03 00:51:25

问题


I have a bunch of html files with different file-names, that I need to add an option to use keyboard arrow keys to navigate (previous and next file).

The file-names are not dynamic.. for example: filename.html, anotherfile.html, thirdone.html etc.

So I need what's in the .js file for the navigation, and what I should link the previous, next buttons on the html file?


回答1:


If you were to define two ID's on two <a> tags like so:

<a id="prev" href="filename.html">prev</a>
<a id="next" href="thirdone.html">next</a>

You could do something like this in navigation.js and include it from every page:

// when the document is ready, run this function
jQuery(function( $ ) {
    var keymap = {};

    // LEFT
    keymap[ 37 ] = "#prev";
    // RIGHT
    keymap[ 39 ] = "#next";

    $( document ).on( "keyup", function(event) {
        var href,
            selector = keymap[ event.which ];
        // if the key pressed was in our map, check for the href
        if ( selector ) {
            href = $( selector ).attr( "href" );
            if ( href ) {
                // navigate where the link points
                window.location = href;
            }
        }
    });
});

You could even use a little CSS to make the #prev, #next { display: none; } or play around with absolutely positioned CSS triangles.



来源:https://stackoverflow.com/questions/12682157/keyboard-navigation-with-arrow-keys-next-and-back-for-changing-filenames

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!