2010-08-26 72 views
2

我試圖關閉一個燈箱,當按下轉義鍵但彈出窗口未關閉時。關閉按ESC時的燈箱

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

    if(e.keyCode==27 && popupStatus==1){ 

     disablePopup(); 
    } 
}); 

下面是完整的代碼:

var popupStatus = 0; 
var buttonDivID = ""; 
var conDivID = ""; 

//determine which div is clicked 
function setDiv(div) { 
    if(div==1){ 
     conDivID = "#intro"; 
    } 
    if(div==2) { 
     conDivID = "#presentation"; 
    } 
} 

//loading popup with jQuery magic! 

function loadPopup(){ 

    //loads popup only if it is disabled 

    if(popupStatus==0){ 

     $("#backgroundPopup").css({ 

      "opacity": "0.7" 

     }); 

     $("#backgroundPopup").fadeIn("slow"); 

     $(conDivID).fadeIn("slow"); 

     $(conDivID).CenterIt(); 

     popupStatus = 1; 

    } 

} 

//disabling popup with jQuery magic! 

function disablePopup(){ 

    //disables popup only if it is enabled 

    if(popupStatus==1){ 

     $("#backgroundPopup").fadeOut("slow"); 

     $(conDivID).fadeOut("slow"); 

     popupStatus = 0; 
     buttonDivID = ""; 
     conDivID = ""; 
    } 
} 



//centering popup 

function centerPopup(){ 

    //request data for centering 

    var windowWidth = document.documentElement.clientWidth; 

    var windowHeight = document.documentElement.clientHeight; 

    var popupHeight = $(conDivID).height(); 

    var popupWidth = $(conDivID).width(); 

    //centering 

    $(conDivID).css({ 

     "position": "absolute", 

     "top": windowHeight/2-popupHeight/2, 

     "left": windowWidth/2-popupWidth/2 

    }); 

    //only need force for IE6 

    $("#backgroundPopup").css({ 

     "height": windowHeight 

    }); 
} 

//CONTROLLING EVENTS IN jQuery 

$(document).ready(function(){ 

    $("#vid2").click(function(){ 
    //set the lightbox divs 
    setDiv(2); 
    loadPopup(); 
    }); 

    //CLOSING POPUP 

    //Click the x event! 

    $(".popupContactClose").click(function(){ 

     disablePopup(); 

    }); 

    //Press Escape event! 

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

     if(e.keyCode==27 && popupStatus==1){ 

      disablePopup(); 
     } 
    }); 
}); 

另一種方法,這是點擊X按鈕正確關閉彈出。爲什麼不關閉它?

+1

您是否檢查了您在按鍵處理程序中獲得的鍵碼? jQuery有'event.which',可以用跨瀏覽器來確定哪個鍵被按下:http://api.jquery.com/event.which/ – Pat 2010-08-26 19:23:03

回答

17

這工作:

$(document).ready(function(){ 
    $(document).bind('keydown', function(e) { 
     if (e.which == 27) { 
      alert('esc pressed'); 
     } 
    }); 
}); 
2

這是一個BUG:退出按鈕不工作jquery.lightbox-0.5

解決方案: http://code.google.com/p/jquery-lightbox/issues/detail?id=27#c4

我不知道,如果這已被燈箱人員修復。我做了一點 調試,發現在 jquery.lightbox-0.5js的第339行上,對於Mozilla瀏覽器,在 行339上,escapeKey被設置爲'undefined'。所以,我加入 以下代碼塊在線341重新設置退出鍵VAR爲「27」:

// Fix because Escape Key wasn't being detected 
    if (escapeKey == undefined) { escapeKey = 27; } 

這應該是右側的「//獲取小寫形式鍵」 以上評論,你可以在文件中快速搜索。

現在適用於我。

+0

其實問題的唯一相關答案「ESC時關閉燈箱被按下「。我不知道爲什麼問題的作者寫了這麼多的代碼。 – 2012-04-17 06:39:27

2

只需使用的KEYUP代替按鍵,而不是的keyCode。 Chrome未使用按鍵返回任何關鍵代碼,並且Firefox返回零。

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

    if(e.which == 27 && popupStatus == 1){ 

     disablePopup(); 
    } 
});