jQuery を使用するナビゲーション メニューを作成しようとしています。キーボード ユーザーがマウス ユーザーと同じエクスペリエンスを持てるようにしたかったので、イベント ハンドラーにある機能hover()
を myfocus()
および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);
}
);
});
ここで確認できます