


comprar viagra online
comprar viagra generico barato
comprar cialis generico online
comprar cialis online seguro
Advanced keypress navigation with jQuery |
A while ago, an article called "A fancy Apple.com-style search suggestion" was placed here. Many people loved it and already used the search suggestion in their latest web project. Still, readers gave some criticism about the script. According to Jape, Simon and others the script was missing one vital element which the Apple search does have: keypress navigation (keyboard accessibility). For that reason, I'm presenting you a way you can improve that script yourself using advanced keypress navigation with jQuery. ![]() Check out the demo so you can try it yourself: There is a simple menu displayed which reacts to the Up and Down arrows, as well as Enter (or Return) to follow the link. The script is (a bit) advanced because of the extra functionality when the user combines the mouse hover and keypresses. Want to know how to create it yourself? Check out the source code or follow these steps in this tutorial. HTML The HTML that I came up with isn't that hard to understand: Simply a division with an ID, containing an unorderd list with links. <div id="menu"> <ul> <li><a href="http://www.marcofolio.net/">Marcofolio.net</a></li> <li><a href="http://feeds2.feedburner.com/marcofolio">Marcofolio RSS feed</a></li> <!-- more items could be added --> </ul> </div> The only thing we need to remember here is the CSS I'm not going to add very many CSS details to this tutorial. All CSS stuff is pretty basic, no fancy-stuff going on. However, there is one CSS class that you should remember: .itemhover { background-color:#b7b7b7 !important; color:#232323 !important; } The Keypress Now it's time to get our hand dirty and get down to the coding part with jQuery. First things first; We'll need to store the current selected item from the menu, including the URL where that menu item links to. So, we'll declare two variables at the top of the script: var currentSelection = 0; var currentUrl = ''; After loading jQuery from Google, we'll need to add a keylistener to the whole page. I've added comments to the script to make it clear. google.load("jquery", "1.3.1"); google.setOnLoadCallback(function() { // Register keypress events on the whole document $(document).keypress(function(e) { switch(e.keyCode) { // User pressed "up" arrow case 38: navigate('up'); break; // User pressed "down" arrow case 40: navigate('down'); break; // User pressed "enter" case 13: if(currentUrl != '') { window.location = currentUrl; } break; } }); }); As you can see, this part requests the Both the Up and Down arrow call a (non-existing) function called
function navigate(direction) { // Check if any of the menu items is selected if($("#menu ul li .itemhover").size() == 0) { currentSelection = -1; } if(direction == 'up' && currentSelection != -1) { if(currentSelection != 0) { currentSelection--; } } else if (direction == 'down') { if(currentSelection != $("#menu ul li").size() -1) { currentSelection++; } } setSelected(currentSelection); } The function first checks if there already is a list item with the class On the first go, the user can only use the Down arrow (Up arrow doesn't have any use at this point). If the Down arrow is used, it first checks if the current selected item isn't already at the bottom of the list (by checking the size). If it isn't, the Almost the same counts for the Up arrow. When pressed, it first checks if it isn't at the top of the list (index: 0). The end of the function calls another function called function setSelected(menuitem) { $("#menu ul li a").removeClass("itemhover"); $("#menu ul li a").eq(menuitem).addClass("itemhover"); currentUrl = $("#menu ul li a").eq(menuitem).attr("href"); } The first line removes all classes inside the list called Now we already have a wonderful menu that reacts to the keypresses from the user (Up, Down and Enter). But what about the mouse movements? Mouse movements We could do this the easy way by adding a To make this also work perfectly with the mouse, we'll enhance the script with some more stuff. First, we'll need to let the menu item "know" at what index there are positioned. This is really simple to achieve, but essential to make this work. When the user hovers the third item, there is no way the item itself would know it is the third. But by adding data, it does: // Add data to let the hover know which index they have for(var i = 0; i < $("#menu ul li a").size(); i++) { $("#menu ul li a").eq(i).data("number", i); } As you can see, this part loops through all the menu items and adds the index of that item (called So, let's add a hover-listener to the menu: // Simulate the "hover" effect with the mouse $("#menu ul li a").hover( function () { currentSelection = $(this).data("number"); setSelected(currentSelection); }, function() { $("#menu ul li a").removeClass("itemhover"); currentUrl = ''; } ); The first parameter from The second function (on mouse leave) isn't that spectacular: It removes all Conclusion and Download That's about it! The whole script can be viewed when downloading the source files and you can test it on the demo page. These kind of techniques can really improve the user friendliness of your website. Of course, scripts can always be improved. What do you want to see differently or do you see any bug fixes? If you're going to use this somewhere on your next project, please share! Tags: keypress navigation jquery webdevelopment Interested in this topic? You might enjoy another article I've written called |
< Prev | Next > |
---|
Search |
---|
Or try the sitemap |