-
Notifications
You must be signed in to change notification settings - Fork 4
/
jquery.jknav.min.js
19 lines (19 loc) · 2.94 KB
/
jquery.jknav.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
jknav
@name jquery.jknav.js
@author Yu-Jie Lin
@version 0.5.3
@date 2016-06-07
@copyright (C) 2010-2013 Yu-Jie Lin <[email protected]>
@copyright (C) 2014 Kyle J. Davis
@copyright (C) 2016 Petr Vacha
@license MIT License
@homepage https://github.com/livibetter-backup/jquery-jknav
@example http://livibetter-backup.github.io/jquery-jknav/jquery.jknav.demo.html
*/
(function(b){function e(c){var a=window.console;b.jknav.DEBUG&&a&&a.log&&a.log("jknav: "+c)}function k(c,a){var d=b.jknav.index[a.name];e("Calculating index for "+a.name+", current index = "+d);if(null==d){var f=b(b.jknav.TARGET).scrollTop();e(b.jknav.TARGET+" top = "+f);b.each(b.jknav.items[a.name],function(a,c){var e=Math.floor(b(c).offset().top);f>=e&&(d=a)});null==d?d=0<c?0:b.jknav.items[a.name].length-1:0<c&&++d>=b.jknav.items[a.name].length?a.circular&&(d=0):0>c&&f==Math.floor(b(b.jknav.items[a.name][d]).offset().top)&&
0>--d&&a.circular&&(d=b.jknav.items[a.name].length-1)}else{if(!a.circular&&(0==d&&-1==c||d==b.jknav.items[a.name].length-1&&1==c))return d;d+=c;d>=b.jknav.items[a.name].length&&(d=0);0>d&&(d=b.jknav.items[a.name].length-1)}e("new index = "+d);return b.jknav.index[a.name]=d}function h(c,a){if("input"==c.target.tagName.toLowerCase()||"button"==c.target.tagName.toLowerCase()||"select"==c.target.tagName.toLowerCase()||"textarea"==c.target.tagName.toLowerCase())e("keyup: "+c.target.tagName+", target is INPUT ignored.");
else{var d=String.fromCharCode(c.keyCode).toLowerCase();e("keyup: "+c.target.tagName+", key: "+d);if(d==a.up.toLowerCase()||d==a.down.toLowerCase()){a.reevaluate&&(b.jknav.index[a.name]=null);var f=k(d==a.down.toLowerCase()?1:-1,a);void 0!==b.jknav.items[a.name][f]&&(d=b(b.jknav.items[a.name][f][0]),b(b.jknav.TARGET).animate({scrollLeft:Math.floor(d.offset().left),scrollTop:Math.floor(d.offset().top)},a.speed,a.easing,function(){var d=b.jknav.items[a.name][f][1];d&&d(b.jknav.items[a.name][f][0])}))}}}
b.fn.jknav=function(c,a){null==a&&(a="default");null==b.jknav.items[a]&&(b.jknav.items[a]=[]);return this.each(function(){b.jknav.items[a].push([this,c]);b.jknav.items[a].sort(function(a,c){var e=b(a[0]).offset().top,g=b(c[0]).offset().top;if(e<g)return-1;if(e>g)return 1;if(e==g)return e=b(a[0]).offset().left,g=b(c[0]).offset().left,e<g?-1:e>g?1:0})})};b.jknav={index:{},items:{},opts:{},default_options:{up:"k",down:"j",name:"default",easing:"swing",speed:"normal",circular:!0,reevaluate:!1},DEBUG:!1,
TARGET_KEYUP:"html",TARGET:navigator.userAgent.toLowerCase().match(/(chrome|webkit)[ \/]([\w.]+)/)?"body":"html",init:function(c){var a=b.extend(b.extend({},b.jknav.default_options),c);b.jknav.index[a.name]=null;b.jknav.opts[a.name]=a;b(b.jknav.TARGET_KEYUP).keyup(function(b){h(b,a)});e('new set "'+a.name+'" initialzed.')},up:function(c){c=b.jknav.opts[c||"default"];h({target:{tagName:""},keyCode:c.up.charCodeAt(0)},c)},down:function(c){c=b.jknav.opts[c||"default"];h({target:{tagName:""},keyCode:c.down.charCodeAt(0)},
c)}}})(jQuery);