简体   繁体   English

按键事件后如何在Fancybox中滚动

[英]How To Scroll Inside Fancybox after Keydown Event

I am using Fancybox 2.1.5 in a ASP.NET MVC4 web application to display a list of string data in an <ul> . 我在ASP.NET MVC4 Web应用程序中使用Fancybox 2.1.5<ul>显示字符串数据列表。 In an attempt to make things easier for the user I have enabled keypress events to navigate up and down the resulting <li> . 为了使用户更轻松,我启用了按键事件来在结果<li>上下移动。 The user can use the mouse to scroll & select or the keyboard to select items and continue on. 用户可以使用鼠标滚动和选择,或使用键盘选择项目并继续。

What I have found is that if the data to be shown is greater than a certain amount and vertical scrollbars are created (note: Fancybox has a maxHeight property set), when using the keyboard to navigate down the list, if the selected item goes off the fancybox window/dialog the scrollbars are not moving to track with it as they would if you were to scroll with the mouse-wheel. 我发现的是,如果要显示的数据大于一定数量并且创建了垂直滚动条(请注意:Fancybox设置了maxHeight属性),则在使用键盘向下导航列表时,如果所选项目不可用在fancybox窗口/对话框中,滚动条并没有像跟踪鼠标滚轮那样移动以进行跟踪。

Does anyone know how I can get Fancybox to scroll manually when I use the keyboard? 有谁知道我使用键盘时如何使Fancybox手动滚动?

I have tried messing about with overflow css settings and fancybox scroll properties but this isnt helping. 我试图弄乱溢出的CSS设置和fancybox滚动属性,但这没有帮助。 Im sure I need to trigger an event when I'm navigating manually.. 我确定我在手动导航时需要触发一个事件。

Html HTML

<div id='myDiv'>
    <ul>
        <li>Apple</li>
        <li>Orange</li>
        <li>Pear</li>
    </ul>
</div>

Fancybox JS inc. 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) { /* Up */
        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 like the fancybox scrollable container (the one with overflow property set to auto ) needs to gain focus before you can use the arrows to scroll it up and down. 似乎fancybox可滚动容器(将overflow属性设置为auto容器)需要获得focus然后才能使用箭头上下滚动它。

As a workaround, use the afterShow callback to set focus on the .fancybox-inner element like : 解决方法是,使用afterShow回调将focus设置在.fancybox-inner元素上,例如:

$(".fancybox").fancybox({
    afterShow: function(){
        $(".fancybox-inner").attr("tabindex",1).focus();
    }
});

IMPORTANT : Notice that we also added the tabindex attribute to make the workaround consistent with most browsers. 重要说明 :请注意,我们还添加了tabindex属性,以使解决方法与大多数浏览器一致。 Chrome won't work without it and Firefox will loose focus if clicking inside the content (when having interactive content for instance.) 如果没有它,Chrome将无法运行,如果在内容中单击(例如,具有交互式内容时),Firefox将失去focus

Check this JSFIDDLE and you will see that you can use the arrows to scroll the content up and down right after fancybox is shown. 选中此JSFIDDLE ,您将看到可以在显示fancybox之后使用箭头上下滚动内容。

Since you are not focusing on any input element, the main div will not (obviously) scroll. 由于您不关注任何输入元素,因此主div不会(显然)滚动。 If you really need to, then you have to check position of highlighted element, calculate if it is visible and scroll parenting element if needed. 如果确实需要,则必须检查突出显示的元素的位置,计算它是否可见,并在需要时滚动育儿元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM