jquery - jQuery focus() 和 Blur() 事件的滞后问题

标签 jquery click mouseevent conflict jquery-events

我正在尝试创建一个使用一些 jQuery 的导航菜单。我希望键盘用户能够获得与鼠标用户相同的体验,因此我在 focus() 中复制了 hover() 事件处理程序中的功能和 blur() 事件处理程序。由于某种原因,当用户单击链接时,这会导致 Firefox 和 IE 出现明显的延迟,而当使用 focus()blur() 代码时,不会发生这种情况。带走。我怎样才能加快速度?我已经在有限的 JavaScript 知识允许的范围内进行了尽可能多的优化,但我没有看到任何“加速”,所以我认为这可能与这些浏览器处理事件的方式有关。

有什么我忽略的重大事情吗?或者是否有其他方法可以在不使用这些事件的情况下保留键盘用户的可访问性?

        var statePad=0;

            function stateChanger(ourStatePad) {
                //the purpose of this function is to translate the current state of the menu into a different graphical representation of the menu state.
                var tempVar=ouStatePad;
                var tempArray = new Array;
                tempArray[5]=0;
                for (var x=0;x < 5;x++) {
                    tempArray[x]=tempVar % 10;
                    tempVar=(tempVar-tempArray[x])/10;
                }
                for (var arrayIndex=4;arrayIndex>=0;arrayIndex--) {
                   //Calculate the proper position in our CSS sprite, based on the each link's state, as well as it's neighbors'.
                    $(".block").eq(4 - arrayIndex)
                    .css(
                        "background-position",
                        //x-position
                        ((4 - arrayIndex) * -100) + "px " + 
                        //y-position
                        (tempArray[arrayIndex] + ((3 * tempArray[(arrayIndex) + 1]) * -30))) + "px";
                }
            }


        function hoverState(index,sign) {
            var placeholder=Math.pow(10,4-index);

            if (statePad != placeholder*2)
                statePad += (placeholder * sign);
            stateChanger(statePad);
}

        .click(function() {
            var index=$("#navbar a").index(this);
            statePad=Math.pow(10,(4-index))*2;
            stateChanger(statePad);
            $(".active").removeClass("active");
            $(this).addClass("active");
        })


        .hover(
            function () {
                hoverState($("#navbar a").index(this),1);
            },
            function () {
                hoverState($("#navbar a").index(this),-1);
            });

        $("#navbar a").focus( 
            function() {
                hoverState($("#navbar a").index(this),1);
            }
        );

        $("#navbar a").blur( 
            function() {
                hoverState($("#navbar a").index(this),-1);
            }
        );  
    });

您可以查看here

最佳答案

有很多不必要的lengthening of the scope chain在您的代码中,较长的作用域链将需要更长的时间来解析。它可以缩短为以下内容

$("navbar a").click(blah) 
             .hover(foo,bar)
             .focus(foo)
             .blur(bar);

希望这能减少明显的延迟。如果进行此更改后仍然看到明显的滞后,请发布事件处理函数的代码,因为也可以对该代码进行改进。

编辑:

为了回应您的评论,您可以使用传入的 event 获取函数中的索引。对象的target属性,这将是引发事件的元素。因此,要获取 <a> 的索引所有 <a> 中的元素<ul> 中的元素通过 id navbar,我们可以使用每个 <a>包含在 <li> 中,因此每种情况下的索引都是相同的。有了这个心,event.target将是<a>引发单击事件的元素,event.target.parentNode将是 <a> 的父元素这是 <li>

要获取索引,您可以使用

function hoverState(e) { 
    // get the index of the <a> element, which will be the same
    // as the index of the <li> that contains it in the <ul>
    //
    var index = $(e.target.parentNode).prevAll().length; 
    //
    // get the sign
    var sign = (e.type === 'mouseenter' || e.type === 'focus')? 1 : -1;
} 

这将消除对包装hoverState的匿名函数事件处理程序的需要。

这是一些修改后的代码

var statePad=0;

// the purpose of this function is to translate 
// the current state of the menu into a different 
// graphical representation of the menu state.
//
function stateChanger(ourStatePad) {

    var tempVar=ourStatePad;
    var tempArray = [0,0,0,0,0];
    for (var x=0;x < 5;x++) {
        tempArray[x]=tempVar % 10;
        tempVar=(tempVar-tempArray[x])/10;
    }
    // Calculate the proper position in our CSS sprite, 
    // based on the each link's state, as well as it's neighbors'
    //
    var arrayIndex=4;
    while (arrayIndex--) {

        $("#rightpostheader div.block").eq(4 - arrayIndex)
            .css(
                "backgroundPosition",
                //x-position
                ((4 - arrayIndex) * -100) + "px " + 
                //y-position
                (tempArray[arrayIndex] + ((3 * tempArray[(arrayIndex) + 1]) * -30))) + "px";
    }

}


function hoverState(e) {
    var index = $(e.target.parentNode).prevAll().length;
    var sign = (e.type === 'mouseenter' || 
                e.type === 'focus')? 1 : -1;
    var placeholder=Math.pow(10,4-index);

    if (statePad != placeholder*2)
        statePad += (placeholder * sign);
    stateChanger(statePad);
}

$("#navbar a")
    .click(function(e) {
        // might be able to rework this into using hoverState too
        var $this = $(e.target);

        // get the index of the parent <li>
        var index= $this.parent().prevAll().length;

        statePad=Math.pow(10,(4-index))*2;

        stateChanger(statePad);

        $("#navbar a").removeClass('active');
        $this.addClass('active');
    })
    .bind("mouseenter mouseleave focus blur", hoverState);  

关于jquery - jQuery focus() 和 Blur() 事件的滞后问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1640277/

相关文章:

python - 如何显示包含本地 Python 函数可点击链接的 Pandas 单元格?

jquery - 如何使用 jQuery 制作选择下拉列表?

jQuery:触发 "mousestop"事件的简单方法

javascript - 如何在可滚动容器上使用 wow.js

jquery - 在不刷新屏幕的情况下,使用 jQuery 更改不透明度在 Chrome 或 Safari 中不起作用

Javascript 点击功能不起作用

java - 单击新位置时如何阻止形状消失?

reactjs - 如何使用 mouseEvent 和 event.target 键入事件(使用 typescript react 项目)?

javascript - 使用 AJAX 将值从 JavaScript 传递到 PHP

jquery - 将输入字段值设置为外来字符