开发者

keyup bindings not working in Firefox

开发者 https://www.devze.com 2022-12-24 05:32 出处:网络
You can see my project here - http://www.inluxphoto.com/custom/jsgallery/index.php I am attempting to make the left and right arrow keys move the slideshow.I was able to get it to work in all browse

You can see my project here -

http://www.inluxphoto.com/custom/jsgallery/index.php

I am attempting to make the left and right arrow keys move the slideshow. I was able to get it to work in all browsers by following exactly the instructions on the front page of jqueryfordesigners.com (sorry I am only allowed one link).

However, it is necessary that the keyup be unbinded until the animation completes, so the user cannot do a quick double tap of the key, which breaks the show. This led me to the following function -

function keyCommands() {

  //Bind Keys according to keyAssignments
  function keyCommandBind() {
   $(document.documentElement).bind('keyup', keyAssignments)   
  }

  //Bind functions to specific keys
  function keyAssignments() {

   if (event.keyCode == 37) {
    leftArrow();
   }

   if (event.keyCode == 39) {
    rightArrow();
   }

   if (event.keyCode == 32) {
    spaceBar();
   }

  }

  f开发者_如何学运维unction leftArrow() {
         //unbind, do stuff, rebind
  }

  function rightArrow() {
          //unbind, do stuff, rebind
  }

  function spaceBar() {
          //unbind, do stuff, rebind
  }

  keyCommandBind();

 }

This works in all browsers except Firefox & Camino. Firebug tells me event (ie event.keyCode) is not defined. That's true, it's not defined, and I understand that. However I don't understand why, if it's not defined, does it work in all other browsers.

How can I appropriately define this? Or, am I doing it wrong?

Any help would be most appreciated, thanks for your time!


Try declaring keyAssignments as function keyAssignments(event) {. I'm not sure why it would work in some but not others, maybe they have a reserved variable that bubbles with the keyup event by default?


So when we do keypress listeners here, this is some code we use:

$('.selector').keypress(function(e) {
var key = e.which || e.keyCode || e.keyChar;
    //etc.
  )};

Basically some browsers call it different things.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号