javascript - How To Scroll Inside Fancybox after Keydown Event -
i using fancybox 2.1.5 in asp.net mvc4 web application display list of string data in <ul>
. in attempt make things easier user have enabled keypress events navigate , down resulting <li>
. user can use mouse scroll & select or keyboard select items , continue on.
what have found if data shown greater amount , vertical scrollbars created (note: fancybox has maxheight property set), when using keyboard navigate down list, if selected item goes off fancybox window/dialog scrollbars not moving track if scroll mouse-wheel.
does know how can fancybox scroll manually when use keyboard?
i have tried messing overflow css settings , fancybox scroll properties isnt helping. im sure need trigger event when i'm navigating manually..
html
<div id='mydiv'> <ul> <li>apple</li> <li>orange</li> <li>pear</li> </ul> </div>
fancybox js inc. keyboard setup
$.fancybox.open($("#mydiv"), { minheight: 0, maxheight: 300, aftershow: function () { $(document).on("keydown", function (e) { setupkeyboardevents(e); }) }, afterclose: function () { $(document).off("keydown"); } }); function setupkeyboardevents(e) { var $selected = $("#mydiv ul li.highlight"), $li = $("#mydiv ul li"); if (e.keycode == 40) { /* down */ if (!$selected.length) { $li.eq(0).addclass('highlight') } else { $selected.removeclass('highlight'); if (!$selected.next().length) { $li.eq(0).addclass('highlight') } else { $selected.next().addclass('highlight'); } } } else if (e.keycode === 38) { /* */ if (!$selected.length) { $li.eq(-1).addclass('highlight') } else { $selected.removeclass('highlight'); if (!$selected.prev().length) { $li.eq(-1).addclass('highlight') } else { $selected.prev().addclass('highlight'); } } } else if (e.keycode == 13) { /* enter */ if ($selected.length) { // continue.. } return false; } }
it seems fancybox scrollable container (the 1 overflow
property set auto
) needs gain focus
before can use arrows scroll , down.
as workaround, use aftershow
callback set focus
on .fancybox-inner
element :
$(".fancybox").fancybox({ aftershow: function(){ $(".fancybox-inner").attr("tabindex",1).focus(); } });
important : notice added tabindex
attribute make workaround consistent browsers. chrome won't work without , firefox loose focus
if clicking inside content (when having interactive content instance.)
check jsfiddle , see can use arrows scroll content , down right after fancybox shown.