3

I have a large HTML page consisting of a long list of tables. Each table is about as large as the screen, but varies slightly in size. I'd like to enable the user to use the PgUp and PgDn keys on the keyboard to quickly browse through the tables. Each press of one of these keys shall move one table up or down and place the table's top exactly at the top of the screen.

Is this possible at all?

I considered there might be an HTML element I haven't heard of yet which represents a page break, so that a browser will jump exactly there if one of the Pg** keys is pressed, but I found none.

Is this possible using some Javascript functionality? I considered installing an event listener for key presses and working everything out myself, but that would be rather cumbersome because my page contains a lot of elements which are display:none (i. e. invisible) and they should of course be skipped.

What would be the right approach to achieve this?

Alfe
  • 56,346
  • 20
  • 107
  • 159
  • _“an HTML element I haven't heard of yet which represents a page break”_ - there are no “page breaks” on a normal screen, so that would not even make much sense. JS would be the right approach, yes. – CBroe Sep 19 '17 at 11:15
  • Yeah, sure. Let's call them "separators" (
    or similar), and an enableable switch which makes PgUp and PgDn jump from "separator" to "separator".
    – Alfe Sep 19 '17 at 12:06

1 Answers1

4

You should give your tables an ID, like #table1 #table2 #table3. You can now navigate to these tables by pasting their ID behind your URL. Lets implement this with javascript:

var currTable = 1;

document.addEventListener('keydown', function(event) {
 if(event.keyCode == 33) {
     // PageUp was pressed
     if (currTable - 1 > 0) {
       currTable--;
       window.location.hash = "#table" + currTable;
     }
 }
 else if(event.keyCode == 34) {
     // PageDown was pressed
     currTable++;
     window.location.hash = "#table" + currTable;
 }
});

This is the basic example. If you want to, you could implement a check if the table with that specific ID exists

I considered installing an event listener for key presses and working everything out myself, but that would be rather cumbersome because my page contains a lot of elements which are display:none (i. e. invisible) and they should of course be skipped.

Check if the element you're trying to access has a display of none. If it has, check the next element, and continue like this.

Roy Berris
  • 1,502
  • 1
  • 17
  • 40
  • 1
    The user may have scrolled down to 6th table after the page has finished loading. Pressing the down key then would take them to the 2nd table instead of 7th. – HBK Sep 19 '17 at 11:34
  • 1
    @HBK That is true, but that wasn't his question. He asked for ways of doing it. I gave him one, not a complete project. But just a way of thinking – Roy Berris Sep 19 '17 at 11:35