1

我正在使用本教程中所示的 jQuery 模态窗口之一。

它适用于基本示例,但是当我进行更改以在页面加载时显示模态窗口时它不起作用,<a href="#dialog" name="modal">Simple Modal Window</a>如果单击它甚至会阻止链接显示弹出模式。

我有关于jsFiddle的例子

我不确定我做错了什么。

我只是想要简单的模式窗口显示在页面加载事件中。

launchWindow('#dialog');  // does not work as shown in that tutorial
4

4 回答 4

2

试试这个:http: //jsfiddle.net/HJUZm/

您可以单击<a>刚刚删除的那个。

$(document).ready(function() {
    var id = $(this).attr('href');

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set height 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, .window').hide();
});    

//if mask is clicked
$('#mask').click(function () {
    $(this).hide();
    $('.window').hide();
});        

$(window).resize(function () {

    var box = $('#boxes .window');

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    //Set height and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});

    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();

    //Set the popup window to center
    box.css('top',  winH/2 - box.height()/2);
    box.css('left', winW/2 - box.width()/2);

});    
});
于 2012-12-27T07:04:07.867 回答
2

在您的代码中,它缺少 function launchWindow()。这是功能:

function launchWindow(id) { 
        //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());
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(2000); 
}
于 2012-12-27T07:14:39.177 回答
2

看看这个它在页面加载时有效,就像你描述的 小提琴一样

Here simply I have removed     `$('a[name=modal]').click(function(e) {` }); 

并设置var id = $('#link').attr('href');了链接 id 是标签的 id

于 2012-12-27T07:15:08.810 回答
0

launchWindow(..)未在您的 javascript 中定义。

而是试试这个:
$('a[name=modal]').click();

于 2012-12-27T07:06:07.493 回答