前端学习<四>JavaScript基础——44-键盘事件

鼠标的拖拽事件

拖拽的流程:

(1)onmousedown:当鼠标在被拖拽元素上按下时,开始拖拽;

(2)onmousemove:当鼠标移动时被拖拽元素跟随鼠标移动;

(3)onmouseup:当鼠标松开时,被拖拽元素固定在当前位置。

鼠标的滚轮事件

onmousewheel:鼠标滚轮滚动的事件,会在滚轮滚动时触发。但是火狐不支持该属性。

DOMMouseScroll:在火狐中需要使用 DOMMouseScroll 来绑定滚动事件。注意该事件需要通过addEventListener()函数来绑定。

键盘事件

事件名

onkeydown:按键被按下。

onkeyup:按键被松开。

注意

  • 如果一直按着某一个按键不松手,那么,onkeydown事件会一直触发。此时,松开键盘,onkeyup事件会执行一次。

  • onkeydown连续触发时,第一次和第二次之间会间隔稍微长一点,后续的间隔会非常快。这种设计是为了防止误操作的发生。

键盘事件一般都会绑定给一些可以获取到焦点的对象或者是document。代码举例:

<span style="background-color:#f8f8f8"><span style="color:#333333">     <span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span>
         <span style="color:#117700"><</span><span style="color:#117700">script</span><span style="color:#117700">></span>
             <span style="color:#000000">document</span>.<span style="color:#000000">onkeydown</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span>(<span style="color:#0000ff">event</span>) {
                 <span style="color:#0055aa">event</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">event</span> <span style="color:#981a1a">||</span> <span style="color:#000000">window</span>.<span style="color:#000000">event</span>;
                 <span style="color:#000000">console</span>.<span style="color:#000000">log</span>(<span style="color:#aa1111">'qianguyihao 键盘按下了'</span>);
             };
 ​
             <span style="color:#000000">document</span>.<span style="color:#000000">onkeyup</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span>() {
                 <span style="color:#000000">console</span>.<span style="color:#000000">log</span>(<span style="color:#aa1111">'qianguyihao 键盘松开了'</span>);
             };
         <span style="color:#117700"></</span><span style="color:#117700">script</span><span style="color:#117700">></span>
 ​
         <span style="color:#117700"><</span><span style="color:#117700">input</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"text"</span> <span style="color:#117700">/></span>
     <span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span></span></span>

判断哪个键盘被按下

可以通过event事件对象的keyCode来获取按键的编码。

此外,event事件对象里面还提供了以下几个属性:

  • altKey

  • ctrlKey

  • shiftKey

上面这三个属性,可以用来判断altctrl、和shift是否被按下。如果按下则返回true,否则返回false。代码举例:

<span style="background-color:#f8f8f8"><span style="color:#333333">     <span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span>
         <span style="color:#117700"><</span><span style="color:#117700">script</span><span style="color:#117700">></span>
             <span style="color:#000000">document</span>.<span style="color:#000000">onkeydown</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span>(<span style="color:#0000ff">event</span>) {
                 <span style="color:#0055aa">event</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">event</span> <span style="color:#981a1a">||</span> <span style="color:#000000">window</span>.<span style="color:#000000">event</span>;
                 <span style="color:#000000">console</span>.<span style="color:#000000">log</span>(<span style="color:#aa1111">'qianguyihao:键盘按下了'</span>);
 ​
                 <span style="color:#aa5500">// 判断y和ctrl是否同时被按下</span>
                 <span style="color:#770088">if</span> (<span style="color:#0055aa">event</span>.<span style="color:#000000">ctrlKey</span> <span style="color:#981a1a">&&</span> <span style="color:#0055aa">event</span>.<span style="color:#000000">keyCode</span> <span style="color:#981a1a">===</span> <span style="color:#116644">89</span>) {
                     <span style="color:#000000">console</span>.<span style="color:#000000">log</span>(<span style="color:#aa1111">'ctrl和y都被按下了'</span>);
                 }
             };
         <span style="color:#117700"></</span><span style="color:#117700">script</span><span style="color:#117700">></span>
     <span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span></span></span>

举例:input 文本框中,禁止输入数字。代码实现:

<span style="background-color:#f8f8f8"><span style="color:#333333">     <span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span>
         <span style="color:#117700"><</span><span style="color:#117700">input</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"text"</span> <span style="color:#117700">/></span>
 ​
         <span style="color:#117700"><</span><span style="color:#117700">script</span><span style="color:#117700">></span>
             <span style="color:#aa5500">//获取input</span>
             <span style="color:#770088">var</span> <span style="color:#0000ff">input</span> <span style="color:#981a1a">=</span> <span style="color:#000000">document</span>.<span style="color:#000000">getElementsByTagName</span>(<span style="color:#aa1111">'input'</span>)[<span style="color:#116644">0</span>];
 ​
             <span style="color:#000000">input</span>.<span style="color:#000000">onkeydown</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span>(<span style="color:#0000ff">event</span>) {
                 <span style="color:#0055aa">event</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">event</span> <span style="color:#981a1a">||</span> <span style="color:#000000">window</span>.<span style="color:#000000">event</span>;
 ​
                 <span style="color:#aa5500">//console.log('qianguyihao:' + event.keyCode);</span>
                 <span style="color:#aa5500">//数字 48 - 57</span>
                 <span style="color:#aa5500">//使文本框中不能输入数字</span>
                 <span style="color:#770088">if</span> (<span style="color:#0055aa">event</span>.<span style="color:#000000">keyCode</span> <span style="color:#981a1a">>=</span> <span style="color:#116644">48</span> <span style="color:#981a1a">&&</span> <span style="color:#0055aa">event</span>.<span style="color:#000000">keyCode</span> <span style="color:#981a1a"><=</span> <span style="color:#116644">57</span>) {
                     <span style="color:#aa5500">//在文本框中输入内容,属于onkeydown的默认行为</span>
                     <span style="color:#770088">return</span> <span style="color:#221199">false</span>; <span style="color:#aa5500">// 如果在onkeydown中取消了默认行为,则输入的内容,不会出现在文本框中</span>
                 }
             };
         <span style="color:#117700"></</span><span style="color:#117700">script</span><span style="color:#117700">></span>
     <span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span>
 ​</span></span>

举例:通过键盘的方向键,移动盒子

代码实现:

<span style="background-color:#f8f8f8"><span style="color:#333333"> <span style="color:#555555"><!DOCTYPE html></span>
 <span style="color:#117700"><</span><span style="color:#117700">html</span><span style="color:#117700">></span>
     <span style="color:#117700"><</span><span style="color:#117700">head</span><span style="color:#117700">></span>
         <span style="color:#117700"><</span><span style="color:#117700">meta</span> <span style="color:#0000cc">charset</span>=<span style="color:#aa1111">"UTF-8"</span> <span style="color:#117700">/></span>
         <span style="color:#117700"><</span><span style="color:#117700">title</span><span style="color:#117700">></</span><span style="color:#117700">title</span><span style="color:#117700">></span>
         <span style="color:#117700"><</span><span style="color:#117700">style</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"text/css"</span><span style="color:#117700">></span>
             <span style="color:#3300aa">#box1</span> {
                 <span style="color:#000000">width</span>: <span style="color:#116644">100px</span>;
                 <span style="color:#000000">height</span>: <span style="color:#116644">100px</span>;
                 <span style="color:#000000">background-color</span>: <span style="color:#770088">red</span>;
                 <span style="color:#000000">position</span>: <span style="color:#221199">absolute</span>;
             }
         <span style="color:#117700"></</span><span style="color:#117700">style</span><span style="color:#117700">></span>
     <span style="color:#117700"></</span><span style="color:#117700">head</span><span style="color:#117700">></span>
     <span style="color:#117700"><</span><span style="color:#117700">body</span><span style="color:#117700">></span>
         <span style="color:#117700"><</span><span style="color:#117700">div</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"box1"</span><span style="color:#117700">></</span><span style="color:#117700">div</span><span style="color:#117700">></span>
 ​
         <span style="color:#117700"><</span><span style="color:#117700">script</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"text/javascript"</span><span style="color:#117700">></span>
             <span style="color:#aa5500">// 使div可以根据不同的方向键向不同的方向移动</span>
             <span style="color:#aa5500">/*</span>
 <span style="color:#aa5500">             * 按左键,div向左移</span>
 <span style="color:#aa5500">             * 按右键,div向右移</span>
 <span style="color:#aa5500">             * ...</span>
 <span style="color:#aa5500">             */</span>
 ​
             <span style="color:#aa5500">//为document绑定一个按键按下的事件</span>
             <span style="color:#000000">document</span>.<span style="color:#000000">onkeydown</span> <span style="color:#981a1a">=</span> <span style="color:#770088">function</span>(<span style="color:#0000ff">event</span>) {
                 <span style="color:#0055aa">event</span> <span style="color:#981a1a">=</span> <span style="color:#0055aa">event</span> <span style="color:#981a1a">||</span> <span style="color:#000000">window</span>.<span style="color:#000000">event</span>;
 ​
                 <span style="color:#aa5500">//定义一个变量,来表示移动的速度</span>
                 <span style="color:#770088">var</span> <span style="color:#0000ff">speed</span> <span style="color:#981a1a">=</span> <span style="color:#116644">10</span>;
 ​
                 <span style="color:#aa5500">//当用户按了ctrl以后,速度加快</span>
                 <span style="color:#770088">if</span> (<span style="color:#0055aa">event</span>.<span style="color:#000000">ctrlKey</span>) {
                     <span style="color:#000000">console</span>.<span style="color:#000000">log</span>(<span style="color:#aa1111">'smyhvae ctrl'</span>);
                     <span style="color:#0055aa">speed</span> <span style="color:#981a1a">=</span> <span style="color:#116644">20</span>;
                 }
 ​
                 <span style="color:#aa5500">/*</span>
 <span style="color:#aa5500">                 * 37 左</span>
 <span style="color:#aa5500">                 * 38 上</span>
 <span style="color:#aa5500">                 * 39 右</span>
 <span style="color:#aa5500">                 * 40 下</span>
 <span style="color:#aa5500">                 */</span>
                 <span style="color:#770088">switch</span> (<span style="color:#0055aa">event</span>.<span style="color:#000000">keyCode</span>) {
                     <span style="color:#770088">case</span> <span style="color:#116644">37</span>:
                         <span style="color:#aa5500">//alert("向左"); left值减小</span>
                         <span style="color:#000000">box1</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span> <span style="color:#981a1a">=</span> <span style="color:#000000">box1</span>.<span style="color:#000000">offsetLeft</span> <span style="color:#981a1a">-</span> <span style="color:#0055aa">speed</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">'px'</span>; <span style="color:#aa5500">// 在初始值的基础之上,减去 speed 大小</span>
                         <span style="color:#770088">break</span>;
                     <span style="color:#770088">case</span> <span style="color:#116644">39</span>:
                         <span style="color:#aa5500">//alert("向右");</span>
                         <span style="color:#000000">box1</span>.<span style="color:#000000">style</span>.<span style="color:#000000">left</span> <span style="color:#981a1a">=</span> <span style="color:#000000">box1</span>.<span style="color:#000000">offsetLeft</span> <span style="color:#981a1a">+</span> <span style="color:#0055aa">speed</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">'px'</span>;
                         <span style="color:#770088">break</span>;
                     <span style="color:#770088">case</span> <span style="color:#116644">38</span>:
                         <span style="color:#aa5500">//alert("向上");</span>
                         <span style="color:#000000">box1</span>.<span style="color:#000000">style</span>.<span style="color:#000000">top</span> <span style="color:#981a1a">=</span> <span style="color:#000000">box1</span>.<span style="color:#000000">offsetTop</span> <span style="color:#981a1a">-</span> <span style="color:#0055aa">speed</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">'px'</span>;
                         <span style="color:#770088">break</span>;
                     <span style="color:#770088">case</span> <span style="color:#116644">40</span>:
                         <span style="color:#aa5500">//alert("向下");</span>
                         <span style="color:#000000">box1</span>.<span style="color:#000000">style</span>.<span style="color:#000000">top</span> <span style="color:#981a1a">=</span> <span style="color:#000000">box1</span>.<span style="color:#000000">offsetTop</span> <span style="color:#981a1a">+</span> <span style="color:#0055aa">speed</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">'px'</span>;
                         <span style="color:#770088">break</span>;
                 }
             };
         <span style="color:#117700"></</span><span style="color:#117700">script</span><span style="color:#117700">></span>
     <span style="color:#117700"></</span><span style="color:#117700">body</span><span style="color:#117700">></span>
 <span style="color:#117700"></</span><span style="color:#117700">html</span><span style="color:#117700">></span>
 ​
 ​</span></span>

上方代码,待改进的地方:

(1)移动盒子时,如果要加速,需要先按方向键,再按Ctrl键

(2)首次移动盒子时,动作较慢。后续如果学习了定时器相关的内容,可以再改进。

写在最后:希望大家可以点个关注点个赞,这对up真的很重要!谢谢!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

被瞧不起的神

谢谢啦,感谢支持|一起努力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值