2011-07-09 49 views
2

我確實看過this quesiton,雖然我可以研究如何實現這一點。從文檔準備就緒之外調用時沒有定義函數

基本的代碼覆蓋是:

jQuery.noConflict(); 
jQuery(document).ready(function($){ 

    $("button").click(function(e){ 
     popupOverlay(); 
     popupDisplay(); 
     e.preventDefault(); 
    }); 

function popupOverlay() { 
     var overlayCss = { 
      "width" : $(document).width(), 
      "height" : $(document).height(), 
      "display" : 'block' 
     } 
     $("#popupOverlay").css(overlayCss); 
    } 


function popupDisplay() { 

    var popup = $("#popup"); 

    etc etc code to work out the central position for popup 

    $(popup).css({ 
     'top': yPosition + 'px', 
     'left': xPosition + 'px', 
     'display' : 'block' 
    }); 

} 

}); 

上面他們工作得很好,但我想,如果窗口是通過將低於上述這種調整重新調用上面的函數:

jQuery(window).resize(function() { 
    popupOverlay(); 
    popupDisplay(); 
}); 

我得到的錯誤是:popupOverlay未定義

我試着將彈出函數移出文檔rea DY但後來我得到的錯誤:$不是一個函數

我必須非常小心使用這個代碼,它不會引起與被網站上已經使用的其他JavaScript庫任何衝突。

+0

「我試着移動文檔的彈出函數,準備好了,然後我得到錯誤」 - 對不起,什麼?我認爲在那裏有幾個拼寫錯誤或拼寫錯誤的自動更正... –

+0

爲什麼不移動jQuery(window).resize(function(){'在ready handler中呢? –

+0

我還有一個新問題如果我關閉彈出窗口,然後調整窗口大小,彈出窗口重新打開。我嘗試添加一個if(「#popup:visible」)但這不起作用 –

回答

2

它不起作用,因爲函數popupOverlaypopupDisplay$(document).ready之內,並且您試圖在聲明範圍之外訪問它。


像這樣重新排列你的代碼。

jQuery.noConflict(); 
// DOM Ready event 
jQuery(document).ready(function ($) { 

    $("button").click(function (e) { 
     popupOverlay(); 
     popupDisplay(); 
     e.preventDefault(); 
    }); 
}); 
//window resize event 
jQuery(window).resize(function() { 
    popupOverlay(); 
    popupDisplay(); 
}); 

//custom functions 
function popupOverlay() { 
    var overlayCss = { 
     "width": $(document).width(), 
     "height": $(document).height(), 
     "display": 'block' 
    } 
    jQuery("#popupOverlay").css(overlayCss); 
} 


function popupDisplay() { 

    var popup = jQuery("#popup"); 

    //etc etc code to work out the central position 
    //for popup 

    jQuery(popup).css({ 
     'top': yPosition + 'px', 
     'left': xPosition + 'px', 
     'display': 'block' 
    }); 

} 
+0

這會導致錯誤:$不是函數 –

+0

@John Magnolia:更新的答案 – naveen

+1

我更喜歡你的答案,因爲它可以使文檔保持清潔 –

1

這應該工作。

jQuery.noConflict(); 
jQuery(document).ready(function(){ 
    var $ = jQuery; 
    $("button").click(function(e){ 
     popupOverlay(); 
     popupDisplay(); 
     e.preventDefault(); 
    }); 

function popupOverlay() { 
     var overlayCss = { 
      "width" : $(document).width(), 
      "height" : $(document).height(), 
      "display" : 'block' 
     } 
     $("#popupOverlay").css(overlayCss); 
    } 


function popupDisplay() { 

    var popup = $("#popup"); 

    etc etc code to work out the central position for popup 

    $(popup).css({ 
     'top': yPosition + 'px', 
     'left': xPosition + 'px', 
     'display' : 'block' 
    }); 

} 

jQuery(window).resize(function() { 
    popupOverlay(); 
    popupDisplay(); 
}); 


}); 
+0

這個工作非常感謝你 –

0

)聲明函數的document.ready之外( 沒有理由有聲明它們,

的「不工作」是由於功能屬於其他名稱空間 如果你願意,你仍然可以使用它們在$(function(){})中也添加句柄給你的事件。

相關問題