jQuery keydown() 太仓促了
本质上,我始终有一个可见元素,并且我使用箭头键来更改同级元素的可见性。
向左箭头
= 显示上一个元素到可见元素,然后隐藏下一个元素。
向右箭头
= 显示可见元素的下一个元素,然后隐藏上一个元素。
问题基本上是:快速按箭头键。
在我的jsfiddle中,当前可见的是4,如果我快速按向右箭头
和向左箭头
我最终得到了数字 3,就好像你慢慢地做(等待所有动画完成)一样,你最终得到了数字 4,就像你应该的那样。
http://jsfiddle.net/lollero/je2fZ/
我想要的是能够按尽可能快地按下箭头键并最终显示正确的数字。
..如果你先按左键,然后再按右键多次,最终会显示所有数字,这很奇怪......这也是不希望的。
Essentially I have one visible element at all times and I'm using arrow keys to change the visibility to sibling elements.
Arrow left
= Shows previous element to the visible one and then hides next element.
Arrow right
= Shows next element to the visible one and then hides previous element.
The problem is basically: Quickly pressing the arrow keys.
In my jsfiddle the current visible one is 4 and if I quickly press Arrow right
and Arrow left
I end up with number 3 where as if you do it slowly ( waiting that all the animations finish up ) you end up with number 4 just like you should.
http://jsfiddle.net/lollero/je2fZ/
What I want is to be able to press the arrow keys as quickly as humanly possible and end up showing the right number.
..and it's weird if you first press left and then right multiple times you end up showing all of the numbers... that isnt desired as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这当然不会是最干净的解决方案,但首先想到的是跟踪变量中选定的元素:
这是一个 工作示例。请注意,一旦到达起点/终点,该解决方案就会循环,并尝试沿同一方向继续。我不知道这是否是你想要的,但修改起来很容易。
This certainly won't be the cleanest solution but the first thing that came to mind was to keep track of the selected element in a variable:
Here's a working example. Note that this solution loops round once you reach the start/end and try to continue in the same direction. I don't know if that's what you're after, but it will be easy to modify.
使用
:visible
以外的内容来标记当前元素,因为:visible
将匹配正在隐藏但尚未隐藏的元素en。 使用类进行演示此处。Use something other than
:visible
to mark the current element, as:visible
will match for elements which are hiding but aren't yet hidden. Demo using a class here.我认为最简单的解决方案似乎是使用
:animated
选择器来检查是否存在动画元素,如果存在,则返回 false。或者,更确切地说,要测试是否有没有正在处理动画,并且只有在没有当前动画时,才对元素进行动画处理:JS Fiddle 演示。
Edited to tidy up the above code, to reduce the nested
if
:JS Fiddle 演示。
参考文献:
:animated
选择器。The easiest solution, I think, seems to be to use the
:animated
selector to check whether there's an animated element present and, if there is, return false. Or, rather, to test that there's not an animation in process and, only if there is no current animation, animate the elements:JS Fiddle demo.
Edited to tidy up the above code, to reduce the nested
if
:JS Fiddle demo.
References:
:animated
selector.