Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For the sake of the OP who has not added jQuery to the list of tags ( I congratulate you ), I'll add a small navigation plugin in pure JavaScript. </p> <p><strong>Disclosure - the credit for the actual logic would go to Eric Priestley of Facebook fame as this was lifted from his Javelin JS library.</strong> </p> <p>I'm posting a link to the fiddle <a href="http://jsfiddle.net/55R39/30/" rel="nofollow">here</a>. I also assume from your question that you already have a <em>search</em> solution in place and are simply looking for a navigation solution.</p> <p>Cross posting from fiddle. I've commented a lot in the code, so should be self explanatory. Though if you do have doubts, get in touch.</p> <p><strong>HTML</strong> Can be anything you desire. For brevity, I've added this</p> <pre><code>&lt;input id='control' type='text' placeholder='Search text here' /&gt; &lt;!-- List of results - hardcoded for now --&gt; &lt;ul id='hardpoint' class='hidden'&gt; &lt;li class='item'&gt;Search item 1&lt;/li&gt; &lt;li class='item'&gt;Search item 2&lt;/li&gt; &lt;li class='item'&gt;Search item 3&lt;/li&gt; &lt;li class='item'&gt;Search item 4&lt;/li&gt; &lt;/ul&gt;​ </code></pre> <p><strong>The code</strong></p> <pre><code>(function() { /** * Helper functions */ var bind = function( context, func, more ){ if (typeof func !== 'function') { throw new Error ("No function supplied to bind function"); } var bound = Array.prototype.slice.call( arguments ).slice(2); if (func.bind) { return func.bind.apply(func, [context].concat(bound)); } return function() { return func.apply(context || window, bound.concat(Array.prototype.slice.call( arguments ))); } }; var getChildElements = function(node) { var childNodes = Array.prototype.slice.call(node.childNodes); childNodes = childNodes.filter(function(item) { return item.tagName &amp;&amp; item.nodeType &amp;&amp; item.nodeType === 1 &amp;&amp; item !== node; }); return childNodes; }; var hasClass = function(node, className) { return ((' ' + node.className + ' ').indexOf(' ' + className + ' ') &gt; -1); }; var alterClass = function(node, className, add) { var has = hasClass(node, className); if (add &amp;&amp; !has) { node.className = node.className.trim() + ' ' + className; } else if (has &amp;&amp; !add) { node.className = node.className.replace( new RegExp('(^|\\s)' + className + '(?:\\s|$)', 'g'), ' '); } }; var getIndex = function( list, element ){ var index = -1; for (var i = 0; i &lt; list.length; i++) { if( list[i] === element ){ index = i; break; } }; return index; }; // Start of plugin // Constructor var ListNavigator = function(config) {}; // This can be moved within constructor if you so prefer ListNavigator.prototype.init = function(config) { // On what element do you want the controls to activate // Pressing up down etc on this element, triggers tha navigation this.control = document.getElementById(config.control); // The list of results that can be navigated this.hardpoint = document.getElementById(config.hardpoint); // A list of items ( usually, childNodes ) of hardpoint // Dynamically populated this.display = getChildElements(this.hardpoint);; // What to set the focus on initially this.focus = -1; // Specify a function to execute when the user chooses a result // Keydown - Return : configured to be the choose event type now this.choose = config.choose; this.selector = config.selector; }; ListNavigator.prototype.run = function() { var controlEvents = ['focus', 'blur', 'keydown', 'input'], mouseEvents = [ 'mouseover', 'mouseout', 'mousedown' ], self = this, selector = this.selector; controlEvents.forEach(function(event) { self.control.addEventListener(event, bind(self, self.handleEvent), false); }); mouseEvents.forEach(function(event) { self.hardpoint.addEventListener(event, bind(self, self.onmouse), true); }); }; // Logic to change the focus on keydown ListNavigator.prototype.changeFocus = function(d) { var n = Math.min( Math.max( -1, this.focus + d ), this.display.length - 1); if (this.focus &gt;= 0 &amp;&amp; this.focus &lt; this.display.length) { alterClass(this.display[this.focus], 'focused', false); } this.focus = n; this.drawFocus(); return true; }; // Set the focus on the targetted element ListNavigator.prototype.drawFocus = function() { var f = this.display[this.focus]; if (f) { alterClass(f, 'focused', true); } }; // Handle mouse events ListNavigator.prototype.onmouse = function(event) { var target = event.target, type = event.type; if ( hasClass( target, this.selector ) ) { if ( type === 'mousedown' ) { // Choose this element this.choose(target); } else if ( type === 'mouseover' ) { // Set the focus to element on which mouse is hovering on this.focus = getIndex( this.display, target ); this.drawFocus(); } else if ( type === 'mouseout' ){ // Reset the display to none this.changeFocus(Number.NEGATIVE_INFINITY); } }; }; ListNavigator.prototype.handleEvent = function(e) { var type = e.type; if (type === 'blur') { this.focused = false; this.hide(); } else { alterClass(this.hardpoint, 'hidden', false); this.update(e); } }; ListNavigator.prototype.hide = function() { this.changeFocus(Number.NEGATIVE_INFINITY); this.display = []; alterClass(this.hardpoint, 'hidden', true); }; ListNavigator.prototype.submit = function() { if (this.focus &gt;= 0 &amp;&amp; this.display[this.focus]) { this.choose(this.display[this.focus]); } else { if (this.display.length) { this.changeFocus(1); this.choose(this.display[this.focus]); } } return false; }; ListNavigator.prototype.update = function(event) { console.log( 'upadte' ); this.display = getChildElements(this.hardpoint); if (event.type === 'focus') { this.focused = true; } var k = event.which; if (k &amp;&amp; event.type == 'keydown') { switch (k) { case 38: if (this.display.length &amp;&amp; this.changeFocus(-1)) { event.stopPropagation(); } break; case 40: if (this.display.length &amp;&amp; this.changeFocus(1)) { event.stopPropagation(); } break; case 13: if (this.display.length) { this.hide(); event.stopPropagation(); this.submit(); } break; case 27: if (this.display.length) { this.hide(); event.stopPropagation(); } break; case 9: // If the user tabs out of the field, don't refresh. return; } } }; window.ListNav = ListNavigator })(); var nav = new ListNav(); nav.init({ control: 'control', hardpoint: 'hardpoint', selector: 'item', choose: function(){ console.log( arguments ); } }); nav.run();​ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload