源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

基于Jquery实现键盘按键监听

  • 时间:2021-04-20 12:02 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:基于Jquery实现键盘按键监听
从NETTUTS看到的文章,效果很不错,有点类似于Flash做出来的效果,[url=http://demo.jb51.net/js/2014/KeyPress/]demo在这里[/url] ,原文 对实现步骤讲得很清楚,我就不多提了,实现效果的逻辑比较简单,也就是slideDown()方法, jquery slideDown()方法,实现滑动效果。
[u]复制代码[/u] 代码如下:
// 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");          });      });  } 
而对键盘按键的监听是用的keypress()方法,其实也没什么难度,不过我们很少在页面上使用按键监听,这个例子比较新奇,值得我们参考,如有必要时,可以在项目里用用。
[u]复制代码[/u] 代码如下:
$(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");          }      });
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部