William George William George - 5 months ago 21
Javascript Question

JQuery Esc Keypress bind

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
#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

Comments