在ESC上关闭JS菜单 [英] Close JS Menu on ESC

查看:267
本文介绍了在ESC上关闭JS菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个简单的滑入式菜单系统.我希望能够通过按 ESC 键关闭菜单.

I am working on a simple slide-in menu system. I'd like to be able to close a menu by hitting the ESC key.

这是我当前的代码: http://jsfiddle.net/3w539Lct/3/

Here's my current code: http://jsfiddle.net/3w539Lct/3/

我的JavaScript的第126行,您可以看到:

Line 126 of my Javascript, you can see:

$( document ).on( 'keydown', function ( e ) {
            if ( e.keyCode === 27 ) { // ESC
                $(".menu-wrap").prop("checked", false);
            }
        });

但是,这不起作用.有人可以帮忙吗?

However, this doesn't work. Can someone help?

推荐答案

我重用了一些现有的函数和变量. 这应该是正确的.

I reused some of your existing functions and variables. This should be correct.

元素$(."menu-wrap")是 div ,并且没有选中的属性.您需要添加逻辑,使其在按下退出按钮时隐藏菜单.

The element $(."menu-wrap") is a div, and doesn't have a checked property. You need to add the logic of hiding the menu on escape button press.

     $( document ).on( 'keydown', function ( e ) {
        if ( e.keyCode === 27 ) { // ESC
             isOpen && classie.remove( bodyEl, 'show-menu' );
        }
    });

这是一个正常工作的 演示

Here's a working demo

这篇关于在ESC上关闭JS菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆