- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将一个游戏从 Javascript/jQuery 重写为 Vue。游戏不使用鼠标,一切都由键盘控制。
玩家使用“向上”和“向下”箭头循环浏览按钮,使用“回车”键进行选择,使用“后退”箭头返回。
每次玩家选择一个选项时,数据都会存储在“游戏”数组中,以便在玩游戏时可以显示正确的游戏和玩家。
所以我的问题是,在 Vue 中构建这个的标准方法是什么?
我读到,如果你在 Vue 项目中使用 jQuery 进行 DOM 操作,那么你就做错了什么,但我不知道有任何其他方法可以让 keypress() 以它的工作方式在所有页面上工作jQuery。
这就是当前页面的构建方式(与 Javascript/jQuery 一起使用):
// this is the first page the player sees
<div class="page1">
<div class="button button_selected" data-link="choosegame">Choose a game</div>
<div class="button" data-link="highscores">Highscores</div>
<div class="button" data-link="settings">Settings</div>
</div>
// if the player selects "choose a game", it shows this page:
<div class="choosegame">
<div class="button" data-link="chooseplayers" data-options="{game:'checkers',players:'2',playersmax:'2'}">Checkers</div>
<div class="button" data-link="chooseplayers" data-options="{game:'bingo',playersmin:'2',playersmax:'4'}">Bingo</div>
<div class="button" data-link="chooseplayers" data-options="{game:'scrabble',players:'2',playersmax:'2'}">Scrabble</div>
</div>
// if the player selects a game (ex. checkers), it shows this page:
<div class="chooseplayers">
<div class="button" data-link="playgame" data-options="{player:'Jimmy'}">Jimmy</div>
<div class="button" data-link="playgame" data-options="{player:'Billy'}">Billy</div>
<div class="button" data-link="playgame" data-options="{player:'Arnold'}">Arnold</div>
</div>
// after players are selected, it shows this page:
<div class="playgame">
PLAYING!
</div>
最佳答案
这是您的请求的完整示例。它太大了,无法解释所有内容,因此如果您需要澄清,请提出问题。
这里我基本上在 window 对象上设置了一个事件监听器并使用 Vue 逻辑处理它。
new Vue({
el: '#app',
data: {
buttons: [
{
'choosegame': 'Choose a game',
'highscores': 'Highscores',
'settings': 'Settings'
},
{
'Checkers': { game: 'checkers', players: '2', playersmax: '2' },
'Bingo': { game: 'bingo', playersmin: '2', playersmax: '4' },
'Scrabble': { game: 'scrabble', players: '2', playersmax: '2' }
},
{
'Jimmy': 'Jimmy',
'Billy': 'Billy',
'Arnold': 'Arnold'
},
],
page: 0, // currentPage
selectedButton: 'choosegame',
game: null, // chosen game and player
player: null
},
methods: {
handleKeyPress: function (e) {
const keyCode = String(e.keyCode || e.code || e.keyIdentifier);
if (keyCode === '13') { // enter
if (this.page < 3) {
if (this.page === 1) this.game = this.selectedButton;
if (this.page === 2) this.player = this.selectedButton;
if (this.page === 0 && this.selectedButton !== 'choosegame') {
console.log('not implemented yet. choose a game instead');
} else {
this.page++;
this.selectedButton = Object.keys(this.currentButtons)[0];
}
}
} else if (keyCode === '38' || keyCode === '40') { // navigate up or down
const buttons = this.buttons[this.page];
const pageKeys = Object.keys(buttons);
const currIndex = pageKeys.findIndex(key => this.selectedButton === key);
const newIndex = (keyCode == 38) // up
? (currIndex === 0)
? pageKeys.length - 1
: currIndex - 1
: (keyCode == 40) // down
? (currIndex === (pageKeys.length - 1))
? 0
: currIndex + 1
: currIndex;
this.selectedButton = pageKeys[newIndex]
}
}
},
computed: {
currentButtons: function () {
return this.buttons[this.page] || [] // current buttons object
}
},
mounted: function () {
// add an event listener for keypress
window.addEventListener('keypress', this.handleKeyPress)
}
});
.button_selected {
background-color: red;
}
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="app">
<div v-if="page == 0">
<div class="button"
v-for="(button, index) in currentButtons"
:key="index"
:class="{'button_selected': index === selectedButton}">{{ button }}
</div>
</div>
<div v-if="page == 1 || page == 2">
<div class="button"
v-for="(button, index) in currentButtons"
:key="index"
:class="{'button_selected': index === selectedButton}">{{ index }}
</div>
</div>
<div v-if="page == 3">
You made it here! {{ player }} gonna play {{ game }}
</div>
</div>
关于javascript - 使用 keypress() 控制所有 Vue 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44831541/
这是我的代码: $( '#Example' ).on( "keypress", function( keyEvent ) { if ( keyEvent.which != 44 ) {
自从我切换到 MAC 以来,我一直讨厌这样一个事实,即我必须使用 cmd+tab 的 alt 键来隐藏窗口。 我知道有一些应用程序(如 witch )可以替代 cmd+tab 功能,但我喜欢当前的界面
我希望在文本框上按键时运行一个函数,因此我有以下代码: $("input[x]").keypress(function() { DoX(); }) 这工作正常,但在我的函数中我
我有一个场景,我想通过进行 Http REST 调用来获取匹配的字符串,将每次击键时输入框中出现的字符串发送到服务器。但它没有按我的预期工作,例如,假设输入框中的当前值为“modul”,如果我在“mo
我刚刚开始使用 p5.js,我喜欢它的简单性,但有一点我无法掌握。 我设置了以下Fiddle : function Player(location, width, height, speed, wei
这里有两个类: public class Cls implements Runnable, KeyListener Thread t; Object obj; public Cls(Thing obj
我将一些程序从学校的计算机(Mac)传输到我的家用电脑。进入我的计算机后,我注意到按键现在在每个程序中都不起作用。我花了几个小时试图找出 KeyPressed 不起作用的原因。两台电脑都使用Eclip
我正在用java为自己制作一个俄罗斯方 block 克隆,作为一个学习项目。然而,我现在陷入了获取左右移动棋子的输入部分。我不确定问题是否出在我的方法中,或者问题出在未调用的 keyPressed 方
基本上我必须为一个项目使用 Processing(并非出于选择)并且遇到了关于一次按下多个键的问题。在 keyPressed() 函数中,我有多个条件,每个都将一个键映射到一个 Action 。这一切
目前我正在使用 jQuery,尤其是该函数 keypress() 这个函数可以告诉我很多关于按下哪个键、在哪里以及何时按下的信息。但什么时候是我的问题:所以这段代码... $(document).ke
我是 JavaScript 新手,在使用“keypress”事件时遇到问题;我目前正在做一个小的跳跃脚本测试,“keydown”和“keyup”事件都起作用,但“keypress”不起作用(我尝试在事
我试图在每次字段更改时执行一个函数。首先,我使用一个简单的 textarea 和一个 div: $("#stuff").keypress(function () { $(
嘿伙计们,可能是一个简单的问题,但在网上找不到任何东西。 我有一个包含搜索结果的列表,我希望能够使用向上和向下键在列表中导航。 if (e.keyCode == 40) { //down
我当前正在通过 .click 事件添加输入,然后想要监听此输入上发生的任何按键。但是,附加内容在插入后不会触发任何事件(即模糊、按键、焦点)。有没有人有什么建议?提前致谢! $("#recipient
我想知道是否可以在表单级别当表单中有控件时处理 KeyPress 事件。 当窗体上没有控件时我可以实现这一点,但是当我添加一些东西时,比如按钮,窗体失去焦点并且我无法将其返回,即使使用 Me.Focu
情况:表单有一个文本框,用户必须在其中键入一些文本。此文本框有一个 KeyPress 事件,在该事件中,窗体的文本属性更改为用户在每次击键时键入的文本。 这是文本框按键事件的代码: private
我正在开发一个应用程序并卡住了。我有一个普通的简单按钮。我想要做的是,如果我单击此按钮,它的行为就像您在软键盘上按空格键一样。 我该怎么做。一定有办法,因为您可以开发软键盘。 所以重复 xP 如果我按
在这个页面上: https://subnetipv4.com/ 如果您单击“IP 地址”列中的任何输入框,然后按“.”或“/”键(句点或斜杠),它会跳转到下一个输入框。 或者至少,它在桌面浏览器上是这
这段代码构建正确,一切似乎都有效,但 key 什么也没做。我认为要么是 Action 监听器,要么是椭圆没有更新。我正在尝试通过初学者 java 游戏编程。我确信这很容易,但我没有捕获它。如果这有所作
我在 jTable 中有一系列文本框和组合框。我使用框中的输入来过滤 JPQL 查询的结果,这些结果显示在 jTable 上。现在,想法是使该过程自动化,以便每次用户在框中键入一个字符时,应用程序都会
我是一名优秀的程序员,十分优秀!