Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<ul>
  <li><button>1</button></li>
  <li><button>2</button></li>
  <li><button>3</button></li>
  <li><button>4</button></li>
  <li><button>5</button></li>
  <li><button>6</button></li>
  <li><button>7</button></li>
  <li><button>8</button></li>
  <li><button>9</button></li>
  <li><button>10</button></li>
  <li><button>11</button></li>
  <li><button>12</button></li>
  <li><button>13</button></li>
  <li><button>14</button></li>
  <li><button>15</button></li>
  <li><button>16</button></li>
  <li><button>17</button></li>
  <li><button>18</button></li>
  <li><button>19</button></li>
  <li><button>20</button></li>
</ul>
</body>
</html>
 
/*
  Gridnav - a way to navigate lists with a keyboard in a
  2D fashion instea of item-by-item
  Copyright (c) 2016 Christian Heilmann
  Licensed under the MIT license:
  http://www.opensource.org/licenses/mit-license.php
  Version:  1.0.0
*/
var Gridnav = function (listelement) {
  var that = this;
  this.list = document.querySelector(listelement);
  if (!this.list) {
    throw Error('List item could not be found');
  }
  this.setcodes = function (amount) {
    that.codes = {
      39:1, 68:1,
      65:-1, 37:-1,
      87:-that.amount, 38:-that.amount,
      83: that.amount, 40:that.amount
    };
  }
  this.getamount = function () {
    that.amount = Math.floor(
      that.list.offsetWidth / that.list.firstElementChild.offsetWidth
    );
    that.setcodes(that.amount);
  }
  if (!this.list.getAttribute('data-element')) {
    this.element = this.list.firstElementChild.firstElementChild.tagName;
  } else {
    this.element = this.list.getAttribute('data-element').toUpperCase();
  }
  if (!this.list.getAttribute('data-amount')) {
    this.getamount();
    window.addEventListener('resize', that.getamount);
  } else {
    this.amount = +this.list.getAttribute('data-amount');
  }
  this.setcodes(this.amount);
  this.all = this.list.querySelectorAll(this.element);
  this.keynav = function(ev) {
    var t = ev.target;
    if (t.tagName === that.element) {
      for (var i = 0; i < that.all.length; i++) {
        if (that.all[i] === t) {
          c = i;
          break;
        }
      }
      if (that.codes[ev.keyCode]) {
        if (that.all[c + that.codes[ev.keyCode]]) {
          that.all[c + that.codes[ev.keyCode]].focus();
        }
      }
    }
  }
  this.list.addEventListener('keyup', this.keynav);
};
var makeitso = new Gridnav('ul');
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
codepo8pro
0viewers