jQuery Esc Keypress bind

William George picture William George · Aug 7, 2011 · Viewed 19k times · Source

I'd like to add a bind in which the Esc key will slide my panel back up. Here is my jQuery code.

$(document).ready(function () {

    $(".port-link-container").click(function () {
        $("div.slider-panel").slideUp("slow");
    });

    $("#wr").click(function () {
        $('html, body').animate({ scrollTop: 450 }, 'slow');
        $("div#wr-large").slideDown("slow");
    });

    $("#sema").click(function () {
        $("div#sema-large").slideDown("slow");
    });

    $(".slider-close").click(function () {
        $('html, body').animate({ scrollTop: 0 }, 'slow');
        $("div.slider-panel").slideUp("slow");
    });
});

Answer

Joseph Marikle picture Joseph Marikle · Aug 7, 2011
#pannel
{
    position:fixed;
    width:100%;
    height:200px;
    background-color:#ddd;
}


<div id="pannel"></div>


$(document).keyup(function(e){

    if(e.keyCode === 27)
        $("#pannel").slideToggle();

});

Try that?

fiddle