From e67d9eb88a93aee4f8e4ec3a0ca6a1397bc55c50 Mon Sep 17 00:00:00 2001 From: harikrishnan Date: Sat, 14 Mar 2015 22:47:16 +0530 Subject: [PATCH] Added support for keybd events --- jquery.slimscroll.js | 60 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/jquery.slimscroll.js b/jquery.slimscroll.js index 190368e..0783661 100644 --- a/jquery.slimscroll.js +++ b/jquery.slimscroll.js @@ -76,7 +76,13 @@ borderRadius: '7px', // sets border radius of the rail - railBorderRadius : '7px' + railBorderRadius : '7px', + + //default tab index + tabIndex: 0, + + //Page scroll threshold + pageScrollThreshold: 10 }; var o = $.extend(defaults, options); @@ -156,6 +162,9 @@ // wrap content var wrapper = $(divS) .addClass(o.wrapperClass) + .attr({ + tabindex: o.tabIndex + }) .css({ position: 'relative', overflow: 'hidden', @@ -308,6 +317,51 @@ if (!o.alwaysVisible) { bar.hide(); } } + //attach keyboard events + attachKeybd(); + function _onKeyPress(e) + { + if (!isOverPanel) { return; } + e = e || window.event; + var delta = 1; + var target = e.target || e.srcTarget || e.srcElement; + if ($(target).closest('.' + o.wrapperClass).is(me.parent())) { + var key = e.key || e.keyIdentifier || e.keyCode; + switch (key) { + case 33://pageup + scrollContent(-delta, true, true, true); + break; + case 34://pagedown + scrollContent(delta, true, true, true); + break; + case 38://alert('up'); + scrollContent(-delta, true); + break; + case 40://alert('down'); + scrollContent(delta, true); + break; + default: return; + } + } + // stop window scroll + if (e.preventDefault && !releaseScroll) { e.preventDefault(); } + if (!releaseScroll) { e.returnValue = false; } + } + function attachKeybd() + { + var parent = me.closest('.'+o.wrapperClass); + parent + .bind('keydown', _onKeyPress) + .bind('focusin', function () { + isOverPanel = true; + showBar(); + hideBar(); + }) + .bind('focusout', function () { + isOverPanel = false; + hideBar(); + }); + } // attach scroll events attachWheel(); @@ -333,7 +387,7 @@ if (!releaseScroll) { e.returnValue = false; } } - function scrollContent(y, isWheel, isJump) + function scrollContent(y, isWheel, isJump, isPageJump) { releaseScroll = false; var delta = y; @@ -363,7 +417,7 @@ if (isJump) { - delta = y; + delta = (isPageJump)? me.scrollTop() + (y * me.outerHeight()) - o.pageScrollThreshold : y; var offsetTop = delta / me[0].scrollHeight * me.outerHeight(); offsetTop = Math.min(Math.max(offsetTop, 0), maxTop); bar.css({ top: offsetTop + 'px' });