我在使用特定脚本时遇到了一些问题。我找到了一个我正在尝试实现的简单模式窗口脚本。最初的功能是它会简单地隐藏/显示带有“静态”内容的窗口 div。这很好,但我需要能够动态指定内容的能力。所以我通过允许.load()函数用于“注入” div 的内容来改变事情,然后脚本将简单地显示窗口正常。但是,当模式关闭时,我使用.empty()清空了 div 的内容,以便能够根据用于打开模型窗口的链接中的属性值动态添加不同的内容。
如果我使用.load()函数而不是将代码硬编码<embed>到 div 的内容中,那么一切都工作得很好,除了窗口不在屏幕中间。如果我对对象进行硬编码,<embed>它会很好地居中并且看起来很棒。
<embed>显然,如果将对象动态注入到 div中,则整体窗口高度/宽度存在差异。我尝试.load()在脚本查找窗口 div 的总宽度/高度之前和之后定位函数,但这似乎没有任何影响。
任何帮助将不胜感激!!
这是脚本:
$(document).ready(function() {  
    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');
        //Get source file for AJAX request
        var source = $(this).attr('src');
        //If source is set, send ajax request, and then load content into window
        if(source != undefined) {
            $('.window').load(source);
        }
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});
        //transition effect     
        $('#mask').fadeIn(1000);    
        $('#mask').fadeTo("slow",0.8);  
        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();
        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);
        //transition effect
        $(id).fadeIn(2000); 
    });
    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask').fadeOut("slow");
        $('.window').fadeOut("slow", function() {       
            $(this).empty();
        });
    });     
    //if mask is clicked
    $('#mask').click(function () {
        $(this).fadeOut("slow");
        $('.window').fadeOut("slow", function() {
            $(this).empty();
        });
    });         
});