当前位置: 编程技术>jquery
Jquery 键盘按键监听与滑动效果的实例
来源: 互联网 发布时间:2014-09-03
本文导语: 1,jquery slideDown()方法,实现滑动效果。 代码: // shows a given element and hides all others function showViaKeypress(element_id) { $(".container").css("display","none"); $(element_id).slideDown("slow"); } // shows proper DIV depending ...
1,jquery slideDown()方法,实现滑动效果。
代码:
// shows a given element and hides all others function showViaKeypress(element_id) { $(".container").css("display","none"); $(element_id).slideDown("slow"); } // shows proper DIV depending on link 'href' function showViaLink(array) { array.each(function(i) { $(this).click(function() { var target = $(this).attr("href"); $(".container").css("display","none"); $(target).slideDown("slow"); }); }); }
2,键盘按键的监听,则使用keypress()方法。
代码:
$(document).keypress(function(e) { switch(e.which) { // user presses the "a" case 97: showViaKeypress("#home"); break; // user presses the "s" key case 115: showViaKeypress("#about"); break; // user presses the "d" key case 100: showViaKeypress("#contact"); break; // user presses the "f" key case 102: showViaKeypress("#awards"); break; // user presses the "g" key case 103: showViaKeypress("#links"); } });