【发布时间】:2010-12-11 01:10:21
【问题描述】:
我正在尝试创建一个使用一些 jQuery 的导航菜单。我希望键盘用户能够拥有与鼠标用户相同的体验,因此我将 hover() 事件处理程序中的功能复制到我的 focus() 和 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
【问题讨论】:
-
你也有stateChanger函数的代码吗?
-
@Rob- 你的演示在我的机器上非常流畅流畅,我认为你指的是导航菜单悬停和突出显示?
-
实际上,Russ 在我的机器上也非常流畅——直到我点击一个链接。然后,点击和“postheader”上的图像更新之间存在延迟。这只是几分之一秒,但很明显。这在 Chrome 或 Safari 中根本不会发生,但在 Firefox 3.5 和 IE 8 上会发生。奇怪的是,它在使用慢得多的 javascript 引擎的 Firefox 2.0 上运行良好。认为这可能与 Windows 7 有关,我在 XP 和 OS X 上进行了尝试,并得到了相同的结果。
标签: jquery click mouseevent conflict jquery-events