Js web page keyboard control page turning method

  • 2020-03-30 04:12:53
  • OfStack

The example of this article describes the Js web page keyboard control page turning method. Share with you for your reference. The specific implementation method is as follows:

Keyboard control page turning effect I think we not see a lot, often in a lot of sites, especially the effect of the album can be directly used to use the keyboard page turning, the principle is very simple, as long as the use of js monitoring whether the user has pressed up and down key can be achieved.

Examples are as follows:

<a id="last" href="<?=$lefturl?>"> The previous chapter </a>
<a id="booklist" href="<?=$booklisturl?>"> Returns the directory </a>
<a id="next" href="<?=$righturl?>"> The next chapter </a>

Js code is as follows:

<script language="javascript">
<!--
last=document . getElementById("last").href;
next=document . getElementById("next").href;
booklist=document . getElementById("booklist").href;
function keyUp(e) {
if(navigator.appName == "Microsoft Internet Explorer")
{
var keycode = event.keyCode;
var realkey = String.fromCharCode(event.keyCode);
}else
{
var keycode = e.which;
var realkey = String.fromCharCode(e.which);
}
if(keycode==39){
window.location.href=next;
}
if(keycode==37){
window.location.href=last;
}
if(keycode==13){
window.location.href=booklist;
}
}
document.onkeydown = keyUp;
//-->
</script>

Today from the Internet to see this function, good ah, later can be in the article, add this function

The solution is also simple: when the user clicks the right arrow key, assign the href attribute of A on the next page to window.location.href.

var $=function(id)
{
    return document.getElementById(id);
}
var hotKey=function(e)
{
    var e =e||event;
    var k = e.keyCode||e.which||e.charCode;//Gets the key code
    if (k == 37)
    {
        if ($("prevPage"))
            window.location.href = $("prevPage").href;
    }
    else if (k == 39)
    {
        if ($("nextPage"))
            window.location.href = $("nextPage").href;
    }
    else if (k == 72)
    {
        if ($("home"))
            window.location.href = $("home").href;
    }
}
document.onkeydown = hotKey;// The or so key


Related articles: