Javascript Opera键盘导航

Javascript Opera键盘导航,javascript,keyboard-shortcuts,opera,Javascript,Keyboard Shortcuts,Opera,我很好奇Opera浏览器是如何实现键盘导航的,您可以在这里执行Shift+↑ , ↓ , ← 或→ 你沿着一行或一列行进 您将如何在JavaScript中实现这一点?这里是一个起点: function getCentrePosition(el) { var x= el.offsetWidth/2, y= el.offsetHeight/2; while (el!==null && el.nodeType==1) { x+= el.offsetLef

我很好奇Opera浏览器是如何实现键盘导航的,您可以在这里执行Shift+↑ , ↓ , ← 或→ 你沿着一行或一列行进


您将如何在JavaScript中实现这一点?

这里是一个起点:

function getCentrePosition(el) {
    var x= el.offsetWidth/2, y= el.offsetHeight/2;
    while (el!==null && el.nodeType==1) {
        x+= el.offsetLeft;
        y+= el.offsetTop;
        el= el.offsetParent;
    }
    return [x, y];
}

function arrowKeyHandler(event) {
    if (event===undefined) event= window.event;
    if (!event.shiftKey) return true;

    // Detect which arrow key is pressed. Make a matrix to rotate each
    // case onto the default case for left-pressed.
    //
    var m;
    if (event.keyCode===37) m= [[1,0], [0,1]]; // left
    else if (event.keyCode===38) m= [[0,1], [-1,0]]; // up
    else if (event.keyCode===39) m= [[-1,0], [0,-1]]; // right
    else if (event.keyCode===40) m= [[0,-1], [1,0]]; // down
    else return true;

    // Find link with shortest distance in left and vertical directions.
    // Disregard any links not left of the current link.
    //
    var pos= getCentrePosition(this);
    var bestlink= null, bestdist= null;
    for (var i= document.links.length; i-->0;) {
        var otherpos= getCentrePosition(document.links[i]);
        var dx= (otherpos[0]-pos[0])*m[0][0] + (otherpos[1]-pos[1])*m[0][1];
        var dy= (otherpos[0]-pos[0])*m[1][0] + (otherpos[1]-pos[1])*m[1][1];
        if (dx>=0) continue;
        var dist= Math.abs(dx)+Math.abs(dy)*3; // arbitrary biasing factor
        if (bestdist===null || dist<bestdist) {
            bestlink= document.links[i];
            bestdist= dist;
        }
    }

    // Focus closest link in that direction, if any
    //
    if (bestlink!==null)
        bestlink.focus();
    return false;
}

// Add to each link on-page, except on Opera which already does it
//
if (!window.opera)
    for (var i= document.links.length; i-->0;)
        document.links[i].onkeydown= arrowKeyHandler;

这是一个简单的技巧,通过查看每个链接的中心点,并将其和当前使用旋转矩阵对方向进行归一化的链接进行比较,来猜测一个方向上的“最佳”链接。您可能可以通过在向左移动时查看链接的右边缘,在向右移动时查看链接的左边缘,等等来改进这一点。也许它应该编译页面上所有可聚焦元素的列表,包括表单字段和带有tabindex的元素,而不仅仅是链接。

这里是一个起点:

function getCentrePosition(el) {
    var x= el.offsetWidth/2, y= el.offsetHeight/2;
    while (el!==null && el.nodeType==1) {
        x+= el.offsetLeft;
        y+= el.offsetTop;
        el= el.offsetParent;
    }
    return [x, y];
}

function arrowKeyHandler(event) {
    if (event===undefined) event= window.event;
    if (!event.shiftKey) return true;

    // Detect which arrow key is pressed. Make a matrix to rotate each
    // case onto the default case for left-pressed.
    //
    var m;
    if (event.keyCode===37) m= [[1,0], [0,1]]; // left
    else if (event.keyCode===38) m= [[0,1], [-1,0]]; // up
    else if (event.keyCode===39) m= [[-1,0], [0,-1]]; // right
    else if (event.keyCode===40) m= [[0,-1], [1,0]]; // down
    else return true;

    // Find link with shortest distance in left and vertical directions.
    // Disregard any links not left of the current link.
    //
    var pos= getCentrePosition(this);
    var bestlink= null, bestdist= null;
    for (var i= document.links.length; i-->0;) {
        var otherpos= getCentrePosition(document.links[i]);
        var dx= (otherpos[0]-pos[0])*m[0][0] + (otherpos[1]-pos[1])*m[0][1];
        var dy= (otherpos[0]-pos[0])*m[1][0] + (otherpos[1]-pos[1])*m[1][1];
        if (dx>=0) continue;
        var dist= Math.abs(dx)+Math.abs(dy)*3; // arbitrary biasing factor
        if (bestdist===null || dist<bestdist) {
            bestlink= document.links[i];
            bestdist= dist;
        }
    }

    // Focus closest link in that direction, if any
    //
    if (bestlink!==null)
        bestlink.focus();
    return false;
}

// Add to each link on-page, except on Opera which already does it
//
if (!window.opera)
    for (var i= document.links.length; i-->0;)
        document.links[i].onkeydown= arrowKeyHandler;

这是一个简单的技巧,通过查看每个链接的中心点,并将其和当前使用旋转矩阵对方向进行归一化的链接进行比较,来猜测一个方向上的“最佳”链接。您可能可以通过在向左移动时查看链接的右边缘,在向右移动时查看链接的左边缘,等等来改进这一点。也许它应该编译页面上所有可聚焦元素的列表,包括表单字段和带有tabindex的元素,而不仅仅是链接。

您还可以调用document.moveFocusUp和document.moveFocusDown等函数。。但我想那会有点作弊,而且只会在歌剧中起作用


这些方法与用于空间导航的内置逻辑相关联,即将焦点移动到链接或按钮Opera考虑在给定方向上的最佳匹配

您还可以调用document.moveFocusUp和document.moveFocusDown等函数。。但我想那会有点作弊,而且只会在歌剧中起作用


这些方法与空间导航的内置逻辑相关联,即将焦点移动到链接或按钮上,Opera认为在给定方向上的最佳匹配

这可以在CSS中快速轻松地完成。请在此阅读:


在本例中,如果焦点在链接1上,并且用户按住Shift键并向右按,则焦点将跳转到链接2。如果用户按住Shift键并向上按,焦点将跳转到链接3,无论链接在页面上的什么位置。

这可以在CSS中快速轻松地完成。请在此阅读:


在本例中,如果焦点在链接1上,并且用户按住Shift键并向右按,则焦点将跳转到链接2。如果用户按住Shift键并向上按,则无论链接在页面上的位置如何,焦点都会跳到链接3。

PortoAllet,您应该更善于接受答案。以上是帮助你的一个令人印象深刻的努力。portoalet,你应该更善于接受答案。以上是一个非常令人印象深刻的帮助你的努力。。