绑定事件处理程序的keydown监听功能JavaScript jQuery

绑定事件处理程序的keydown监听功能JavaScript jQuery

问题描述:

我正在尝试将一个处理程序绑定到一个事件。事件是一个键控功能。处理程序将侦听命中变量以产生两个条件之一。第一个条件(奇数点击)将执行1个功能,第二个(偶数次命中)将执行另一个功能。要详细说明,第一个功能将滚动到一个元素,第二个将滚动到另一个元素

I am trying to bind a handler to an event. The event is a keydown function. The handler will listen for hit variables to produce one of two conditions. The 1st condition (odd number of hits) will perform 1 function, the 2nd (even number of hits) will perform another function. To elaborate, the 1st function will scroll to one element, the 2nd will scroll to another element

FIDDLE

以上是一个演示的链接,您可以看到一个讨厌的错误。

Above is a link to a demo, there is a nasty bug which you can see.

唯一可以想到的是,第二个事件应修改以下内容:

The only thing I can think of is that the following should be revised for the second event:

.offset().top


声明命中在您的keydown功能之外,因此每次不会重置为0。

declare hits outside your keydown function so it doesn't get reset to 0 each time.