Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
- <style>
- li.selected {background:yellow}
- </style>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
- </head>
- <body>
- <script>
- $(document).ready(function(){
- var li = $('li');
- var liSelected;
- $(window).keydown(function(e){
- if(e.which === 40){
- if(liSelected){
- liSelected.removeClass('selected');
- next = liSelected.next();
- if(next.length > 0){
- liSelected = next.addClass('selected');
- }else{
- liSelected = $(li).first().addClass('selected');
- }
- }else{
- liSelected = $(li).first().addClass('selected');
- }
- }else if(e.which === 38){
- if(liSelected){
- liSelected.removeClass('selected');
- next = liSelected.prev();
- if(next.length > 0){
- liSelected = next.addClass('selected');
- }else{
- liSelected = li.last().addClass('selected');
- }
- }else{
- liSelected = li.last().addClass('selected');
- }
- }
- });
- });
- </script>
- <ul>
- <li>Item 1</li>
- <li>Item 2</li>
- <li>Item 3</li>
- </ul>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement