[英]Alternative for large switch statment
I have bit of a problem.我有点问题。 I have app with large amount of buttons that i wanna trigger on keyboard press.
我有一个带有大量按钮的应用程序,我想在按下键盘时触发这些按钮。 Currently i'm using switch statemnt for it, but as I want to get more and more buttons (about 25) switch statment gets larger and larger.
目前我正在为它使用 switch statemnt,但随着我想要获得越来越多的按钮(大约 25 个),switch statment 变得越来越大。 How can I replace my switch statment and keep my functionality?
如何替换我的 switch 语句并保持我的功能?
The code:代码:
<button id="left_button" action-name="left" class="button"></button>
<button id="enter_button" action-name="enter" class="button"></button>
<button id="right_button" action-name="right" class="button"></button>
<button id="return_button" action-name="return" class="button"></button>
<button id="down_button" action-name="down" class="button"></button>
function animateButton(div_id) {
div_id.classList.add("boxshadow");
var data_action = div_id.getAttribute("action-name")
//this sends data further down the pipeline
RequestPress(data_action)
setTimeout(function() {
div_id.classList.remove("boxshadow");
}, 400);
}
var up_button = document.getElementById('up_button')
var right_button = document.getElementById('right_button')
var left_button = document.getElementById('left_button')
var down_button = document.getElementById('down_button')
var enter_button = document.getElementById('enter_button')
var esc_button = document.getElementById('return_button')
document.addEventListener('keydown', function(e) {
switch (e.keyCode) {
case 38:
animateButton(up_button)
break;
case 39:
animateButton(right_button)
break;
case 40:
animateButton(down_button)
break;
case 37:
animateButton(left_button)
break;
case 13:
animateButton(enter_button)
break;
case 27:
animateButton(esc_button)
break;
}
});
note: I don't want to use jQuery注意:我不想使用 jQuery
You can iterate through children of the wrapper div of these buttons and make a look up dictionary where key is name
of the button and value is the id
:您可以遍历这些按钮的包装器 div 的子项,并创建一个查找字典,其中键是按钮的
name
,值是id
:
const el = document.getElementById("buttons").children;
const dict = Array.from(el).reduce((a, {id, name}) => ({...a, [name]: id}),{});
and inside the EventListener
just look for the key already in the dictionary as:在
EventListener
内部,只需查找字典中已有的键:
document.addEventListener('keydown', function(e) {
if (dict[e.keyCode]) {
animateButton(document.getElementById(dict[e.keyCode]));
}
});
Here is an example:这是一个例子:
const el = document.getElementById("buttons").children; const dict = Array.from(el).reduce((a, {id, name}) => ({...a, [name]: id}),{}); console.log(dict);
<div id="buttons"> <button id="left_button" name=37 class="button"></button> <button id="right_button" name=39 class="button"></button> <button id="down_button" name=40 class="button"></button> </div>
Note: as you said you only have 25 buttons, so it's not a big deal for a modern machine to loop through 25 items.注意:正如您所说,您只有 25 个按钮,因此对于现代机器来说循环 25 个项目并不是什么大问题。
Hope it helps!希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.