Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts

    Using $(window).keydown but disabling the key pressed and error

    Code:
    		$(window).keydown(function(e){
    			if( e.keyCode == 9 ) { // 9 = Tab
    				$('*').animate({scrollTop: ($(window).scrollTop() + 500)+'px'}, 1000);
    				return false;
    			}
    		});
    Every time I press my tab button, it would do the event BUT it would stick to its position and refrain from moving. In other words, I click tab. It would move the page down by 500 pixels. I try to scroll down, but I can't (I'm using FF 3.0.8)

    What's wrong?

    ALSO when I click the tab button, the tab is still activated even after I returned false. What's wrong with that as well?
    Last edited by Apothem; 04-09-2009 at 05:23 PM.

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    I don't know if using the universal selector is appropriate, but I managed to make up an alternative with the following code.
    Code:
    $('html').keydown(function(event){
      if (event.keyCode === 9)
      {
        $('html').animate({
          scrollTop: $('html').scrollTop() + 500 + 'px'
        });
    
        return false;
      }
    });
    Preview: http://pastebin.me/49dd9b1b4aa77

    Also, just an idea, maybe make it so key + tab will scroll back to the top or when you're at the bottom and press the tab again, it'll scroll to the top.

    Goodluck.

    Edit: I've found that it doesn't work for Safari and Google Chrome (webkit) so here's the fix for it.
    Code:
    $(document).keydown(function(event){
      if (event.keyCode === 9)
      {
        $('html, body').animate({
          scrollTop: $(document).scrollTop() + 500 + 'px'
        });
    
        return false;
      }
    });
    Last edited by Iszak; 04-09-2009 at 10:11 AM.

  • Users who have thanked Iszak for this post:

    Apothem (04-09-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •