Giter VIP home page Giter VIP logo

jquery-key-selection's Introduction

#jQuery keySelection

jQuery keySelection is a lightweight jQuery plugin (3kb) for cycling through arbitrary element collections with arrow keys. Collection members are selectable by keyboard or mouse.

keySelection will always reflect the current dom state, so you can dynamically add elements to the dom and they will be added to the keySelection element collection if they have the required .selection-item class.

##demo & download

See here for a demo.

Download the production version (minified) or the development version.

##basic usage:

keySelection adds a ".selected" class to an element if it is selected with the enter key, the space key or by a mouse click. It adds a ".key-hover" class to an element if it is chosen with the arrow keys or if it is selected. You need to add your own css, to see the effects of keySelection.

    <div class="container">
	    <p class="selection-item">I'm selectable!</p>
	    <ul>
		    <li class="selection-item">Me too!</li>
		    <li>I'm not selectable</li>
	    </ul>
    </div>
$(".container").keySelection();

##options (with default values):

    $(".container").keySelection({
    	exclusiveKeyListener: true, //prevent event propagation for enter, space, up, down, right, left
	    scrollToKeyHoverItem: true,
	    scrollContainer: "html,body",
	    scrollMargin: 10, //space above item, after scrolling
	    scrollAnimationDuration:150,	
	    selectionItemSelector:".selection-item",
        keyActions:[ //use any and as many keys you want. available actions: "select", "up", "down"
            {keyCode:13, action:"select"}, //enter
            {keyCode:38, action:"up"}, //up
            {keyCode:40, action:"down"}, //down
            {keyCode:37, action:"up"}, //left
            {keyCode:39, action:"down"}, //right
            {keyCode:9, action:"down"}, //tab
            {keyCode:32, action:"select"} //space
        ]        
    });

##events keySelection triggers events if an element is selected or keyHovered:

    $(".container").on("keySelection.keyHover", function(e){console.log(e.keyHoverElement);});
    $(".container").on("keySelection.selection", function(e){console.log(e.selectedElement);});

##api

    $(".container").keySelection("stop"); //stop keySelection
    $(".container").keySelection("start"); //start it again
    $(".container").keySelection("up"); //set previous element to .key-hover
    $(".container").keySelection("down"); //set next element to .key-hover
    $(".container").keySelection("select"); //select .key-hover element
    $(".container").keySelection("destroy"); //destroy keySelection for this element

##motivation & alternatives

I needed a lightweight arrow key plugin for arbitrary and dynamically changing element collections (not only lists). I didn't find one, so I made my own.

If you are looking for more advanced features, I recommend Selectonic.

##license

The MIT License (MIT)

Copyright (c) 2014 Christian Voigt

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.