简单的jQuery模态窗口没有在页面加载时显示弹出窗口

时间:2012-12-27 06:54:16

标签: javascript jquery html

我正在使用jQuery模态窗口之一,如this tutorial所示。

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

我在jsFiddle

上有例子

我不确定我做错了什么。

我只是希望简单的模态窗口显示在页面加载事件上。

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

4 个答案:

答案 0 :(得分: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);

});    
});

答案 1 :(得分:2)

在您的代码中,它缺少函数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); 
}

答案 2 :(得分:2)

如你所述,检查它是否适用于页面加载 Fiddle

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

并设置了var id = $('#link').attr('href');,其中链接ID是标记的ID

答案 3 :(得分:0)

您的javascript中未定义

launchWindow(..)

反而试试这个:
$( '一个[名称=模态]')点击();